浏览器端 node.js 还是非阻塞 javascript?
我对非阻塞架构很着迷。虽然我没有使用过 Node.js,但我从概念上掌握了它。另外,我一直在开发一个事件驱动的 Web 应用程序,因此我对事件编程有基本的了解。
如何在浏览器中编写非阻塞 JavaScript?我想这在某些方面一定与 Node 的做法有所不同。例如,我的应用程序允许用户加载大量数据(序列化为 JSON)。解析该数据以重建应用程序状态。这是一个繁重的操作,可能会导致浏览器锁定一段时间。
我相信使用网络工作者是一种方法。 (这似乎是显而易见的选择,但是,我相信 Node 实现了非阻塞、事件驱动的架构,而无需使用 Web Workers,所以我想一定还有另一种方法。)我相信计时器也可以发挥作用。我读到了 TameJS 和其他一些扩展 javascript 语言的库。我对使用原生 javascript 而不引入新语言语法的 javascript 库感兴趣。
非常感谢资源、图书馆和实际示例的链接。
编辑:
了解了更多,我意识到我所说的属于“期货”一词。 然而,jQuery 实现了这一点,它总是使用 XHR 来调用服务器,服务器在返回结果之前进行处理,而我要做的就是在不调用服务器的情况下做同样的事情,客户端在非阻塞中进行处理。方式。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
三是在浏览器上进行非阻塞工作的两种方法
穷人的线程:
您可以使用一些技巧:
setTimeout
模拟时间拼接。这基本上意味着,在完成每个“块”工作后,您可以通过调用 setTimeout(doMore, 10) 为浏览器提供一些响应空间。这基本上是以一种非常糟糕的非优化方式编写您自己的进程调度程序,使用 Web Worker,而不是Three are two methods of doing non-blocking work on the browser
Poor man's threads:
There are a few hacks you can use:
setTimeout
. This basically means that after every "lump" of work you give the browser some room to be responsive by callingsetTimeout(doMore, 10)
. This is basically writing your own process scheduler in a really poor non optimized manner, use web workers instead具体来说,非阻塞是什么意思?
最长的操作,Ajax 调用,已经是非阻塞的(异步)。
如果您需要一些长时间运行的函数来运行“某处”然后执行某些操作,您可以调用
该函数并调用回调。
您还可以阅读 承诺 和 这里也
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
and call the callback from the function.
And you can also read on promises and here as well