promise.all有用,因为javascript是在一个线程中执行的吗? [英] Is promise.all useful given that javascript is executed in a single thread?

查看:313
本文介绍了promise.all有用,因为javascript是在一个线程中执行的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在kriskowal的Q中,人们可以这样做:

In, for example, kriskowal's Q, one can do something like:

promise1.then(function(p1){
  var p2 = makePromise2();
  var p3 = makePromise3();
  var p4 = makePromise4();
  return [p2, p3, p4];
})
.all(promises, function(){
  console.log('all promises fulfilled');
}, function(reason){
  console.log('a promise was rejected: ' + reason.toString());
});

鉴于javascript在单个线程中执行,这是否有任何好处,性能或其他方面,而不仅仅是做一系列then()调用?

Given that javascript executes in a single thread, does this have any benefit, performance or otherwise, over simply doing a series of then() calls?

推荐答案

首先,JavaScript作为一种语言并没有说明并发性。实际上,在很多场景中,您可以创建和运行多线程JavaScript - Web中的WebWorkers,节点中以及自己在现有应用程序中集成JS引擎时。

First of all - JavaScript as a language does not say anything about concurrency. In fact, in a lot of scenarios you can create and run multithreaded JavaScript - WebWorkers in the web, in node, and when integrating a JS engine in an existing app yourself.

事实上,JavaScript语言中没有任何异步(直到ES2015)。但是,实际上 - JavaScript与世界交互的主机环境实现了异步API。

In fact, there is nothing asynchronous in the JavaScript language (Until ES2015). However, in practice - host environments which are how JavaScript interacts with the world implement asynchronous APIs.

虽然JavaScript DOM代码的执行是单线程的,但I / O操作实际上运行在不同的线程和事件上通知JavaScript线程的更改。有时,使用操作系统工具进行异步并发,就像Windows中的IOCP(I / O完成端口)一样。

While execution of JavaScript DOM code is single threaded, I/O operations actually run on a different thread and events notify the JavaScript thread of changes. Sometimes, using an operating system facility for asynchronous concurrency completely like IOCP (I/O completion ports) in Windows.

让我们以AJAX为例。假设我有一个URL列表,我将每个URL映射到XHR调用。

Let's take AJAX for example. Let's say I have a list of URLs and I map each of them to an XHR call.

// with all
var a = ["url1","url2","url3"].map(makeAjaxPromise); // make 3 ajax calls
Promise.all(a).spread(function(res1,res2,res3){ // Q.all with Q
     alert("Everything loaded!");
});

这里,一次进行3次ajax调用。即使JavaScript在这种情况下在单个线程中运行 - 请求都是并行完成的,并且一旦完成使用事件循环就会通知线程,该事件循环在事件完成时通知代码,这反过来解决了承诺。

Here, 3 ajax calls are made at once. Even though JavaScript runs in a single thread in this case - the requests are all made in parallel, and the thread gets notified once they are complete using an "event loop" that notifies code when events completed which in turn resolves the promises.

但是,当你这样做时

 makeAjaxPromise("url1").
 then(makeAjaxPromise.bind(null,"url2").
 then(makeAjaxPromise.bind(null,"url3").then(function(){
      alert("Everything loaded!"); // disregarding getting the data here
 });

它会使一个请求,等待它完成,再做一个,等待它,然后再做第三个请求然后解决。

It'll make a single request, wait for it to complete, make another one, wait for it, make a third one only then and only then resolve.

所以 - 第一种情况:

So - first case:


  • JavaScript线程进行3次DOM API调用

  • DOM API获取这些调用并发出XHR请求,它将控制权交还给JavaScript 立即

  • 当这些请求准备就绪时,它会通知JavaScript,如果可以自由运行,它会运行处理程序。

第二种情况


  • JavaScript线程进行DOM API调用。

  • DOM API获取调用,发出XHR请求,将控制权交还给JavaScript。

  • 当第一个请求准备就绪时,JavaScript会收到通知,它运行nex t in line:
    -JavaScript线程进行DOM API调用。

  • DOM API被调用,发出XHR请求,将控制权交还给JavaScript。

  • 当第二个请求准备就绪时,JavaScript会收到通知并运行下一行:
    -JavaScript线程进行DOM API调用。

  • DOM API获取调用,发出XHR请求,将控制权交还给JavaScript。

  • 当第三次请求准备就绪时,JavaScript会收到通知,然后运行下一行:

  • 三个都准备好了。

  • JavaScript thread makes a DOM API call.
  • DOM API gets call, makes an XHR request, yields control back to JavaScript.
  • When the first request is ready, JavaScript gets notified and it runs the next in line: -JavaScript thread makes a DOM API call.
  • DOM API gets call, makes an XHR request, yields control back to JavaScript.
  • When the second request is ready, JavaScript gets notified and it runs the next in line: -JavaScript thread makes a DOM API call.
  • DOM API gets call, makes an XHR request, yields control back to JavaScript.
  • When the thirdrequest is ready, JavaScript gets notified and it runs the next in line:
  • All three ready.

所以在第二种情况下,请求不是并行的,JavaScript必须等待更多,可能是原来的三倍。

So in the second case, the requests are not made in parallel and JavaScript has to wait a lot more, possibly three times as much.

这篇关于promise.all有用,因为javascript是在一个线程中执行的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆