Search code examples
javascriptnode.jsnonblockingevent-driven

Browser-side node.js or non-blocking javascript?


I am fascinated with non-blocking architectures. While I haven't used Node.js, I have a grasp of it conceptually. Also, I have been developing an event-driven web app so I have a fundamental understanding of event programming.

How do you write non-blocking javascript in the browser? I imagine this must differ in some ways from how Node does it. My app, for example, allows users to load huge amounts of data (serialized to JSON). This data is parsed to reconstitute the application state. This is a heavy operation that can cause the browser to lock for a time.

I believe using web workers is one way. (This seemed to be the obvious choice, however, Node accomplishes a non-blocking, event-driven architecture I believe without using Web Workers so I guess there must be another way.) I believe timers can also play a role. I read about TameJS and some other libraries that extend the javascript language. I am interested in javascript libraries that use native javascript without introducing a new language syntax.

Links to resources, libraries and practical examples are most appreciated.

EDIT:

Learned more and I realize that what I am talking about falls under the term "Futures". jQuery implements this however, it always uses XHR to call a server where the server does the processing before returning the result and what I am after is doing the same thing without calling the server, where the client does the processing but in a non-blocking manner.

http://www.erichynds.com/jquery/using-deferreds-in-jquery/


Solution

  • What do you mean by non-blocking specifically?

    The longest operations, Ajax-calls, are already non-blocking (async)

    If you need some long-running function to run "somewhere" and then do something, you can call

     setTimeout(function, 0)
    

    and call the callback from the function.

    And you can also read on promises and here as well