开发者

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

开发者 https://www.devze.com 2023-04-12 19:53 出处:网络
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 understand

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 t开发者_Go百科hing 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/


Three are two methods of doing non-blocking work on the browser

  • Web Workers. WebWorkers create a new isolated thread for you to do computation in, however browser support tells you that IE<10 hates you.
  • not doing it, expensive work in a blocking fashion should not be done on the client, send an ajax request to a server to do this, then have the server return the results.

Poor man's threads:

There are a few hacks you can use:

  • emulate time splicing by using setTimeout. This basically means that after every "lump" of work you give the browser some room to be responsive by calling setTimeout(doMore, 10). This is basically writing your own process scheduler in a really poor non optimized manner, use web workers instead
  • creating a "new process" by creating a iframe with it's own html document. In this iframe you can do computation without blocking your own html document from being responsive.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号