浏览器端 node.js 还是非阻塞 javascript?

发布于 2024-12-09 01:38:36 字数 730 浏览 0 评论 0原文

我对非阻塞架构很着迷。虽然我没有使用过 Node.js,但我从概念上掌握了它。另外,我一直在开发一个事件驱动的 Web 应用程序,因此我对事件编程有基本的了解。

如何在浏览器中编写非阻塞 JavaScript?我想这在某些方面一定与 Node 的做法有所不同。例如,我的应用程序允许用户加载大量数据(序列化为 JSON)。解析该数据以重建应用程序状态。这是一个繁重的操作,可能会导致浏览器锁定一段时间。

我相信使用网络工作者是一种方法。 (这似乎是显而易见的选择,但是,我相信 Node 实现了非阻塞、事件驱动的架构,而无需使用 Web Workers,所以我想一定还有另一种方法。)我相信计时器也可以发挥作用。我读到了 TameJS 和其他一些扩展 javascript 语言的库。我对使用原生 javascript 而不引入新语言语法的 javascript 库感兴趣。

非常感谢资源、图书馆和实际示例的链接。

编辑:

了解了更多,我意识到我所说的属于“期货”一词。 然而,jQuery 实现了这一点,它总是使用 XHR 来调用服务器,服务器在返回结果之前进行处理,而我要做的就是在不调用服务器的情况下做同样的事情,客户端在非阻塞中进行处理。方式。

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

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/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南…巷孤猫 2024-12-16 01:38:36

三是在浏览器上进行非阻塞工作的两种方法

  • 网络工作者。 WebWorkers 创建一个新的隔离线程供您进行计算,但是 浏览器支持 告诉您 IE< ;10 恨你。
  • 如果不这样做,就不应该在客户端上以阻塞方式完成昂贵的工作,而是向服务器发送 ajax 请求来执行此操作,然后让服务器返回结果。

穷人的线程

您可以使用一些技巧:

  • 使用 setTimeout 模拟时间拼接。这基本上意味着,在完成每个“块”工作后,您可以通过调用 setTimeout(doMore, 10) 为浏览器提供一些响应空间。这基本上是以一种非常糟糕的非优化方式编写您自己的进程调度程序,使用 Web Worker,而不是
  • 通过使用自己的 html 文档创建 iframe 来创建“新进程”。在此 iframe 中,您可以进行计算,而不会阻止您自己的 html 文档响应。

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.
瞳孔里扚悲伤 2024-12-16 01:38:36

具体来说,非阻塞是什么意思?

最长的操作,Ajax 调用,已经是非阻塞的(异步)。

如果您需要一些长时间运行的函数来运行“某处”然后执行某些操作,您可以调用

 setTimeout(function, 0)

该函数并调用回调。

您还可以阅读 承诺这里也

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文