简体   繁体   English

Promise.all() 是按顺序运行还是并行运行?

[英]Does Promise.all() run in sequential or parallel?

Does Promise.all() run in sequential or parallel in Javascript? Promise.all() 在 Javascript 中是按顺序运行还是并行运行?

For Example:例如:

    const promises = [promise1(), promise2(), promise3()]
    Promise.all(promises)
    .then(data => {
        // whatever 
    });

Does promise1() execute and resolve before moving onto promise2() or does promise1() , promise2() , and promise 3() all run in parallel at the same time? promise1()在进入 promise2() 之前执行并解析还是promise1()promise2()promise 3()都同时并行运行? I would assume like Node, Javascript in the browser to be single threaded thus they don't run in parallel?我会假设浏览器中的 Node、Javascript 是单线程的,因此它们不会并行运行?

Javascript is a single threaded application. Javascript 是一个单线程应用程序。 However, asynchronous calls allow you to not be blocked by that call.但是,异步调用允许您不被该调用阻塞。 This is particularly useful when making REST API calls.这在进行 REST API 调用时特别有用。 For example your promise1() can make a REST API call and before it waits for the results, another REST API call can be made from promise2() .例如,您的promise1()可以进行 REST API 调用,并且在等待结果之前,可以从promise2()进行另一个 REST API 调用。 This pseudo-parallelism is thus achieved by not waiting on API servers to do the tasks and fire multiple such calls to either same or different API endpoints in parallel.因此,这种伪并行是通过不等待 API 服务器完成任务并并行地向相同或不同的 API 端点发出多个此类调用来实现的。 This allows your code to continue executing that parts that are not dependent on resolution of the promises.这允许您的代码继续执行不依赖于承诺解析的部分。

So yes, promise1() , promise2() and promise3() can be said to be running in parallel in that respect.所以是的, promise1()promise2()promise3()在这方面可以说是并行运行的。 And there is a chance that promise2() gets resolved before promise1() and so on.还有一个机会, promise2()得到解决之前, promise1()等。 The function Promise.all() waits for all the promises provided to it to fulfill or at least one of them to fail.函数Promise.all()等待提供给它的所有承诺完成或至少其中之一失败。

Learn more about Javascript event loops in this video by Jake Archibald.在 Jake Archibald 的此视频中了解有关 Javascript 事件循环的更多信息。

Promise.all does not make your promises run in parallel. Promise.all不会让你的承诺并行运行。
Promise.all does not make your promises run at all. Promise.all根本不会运行你的承诺。
What Promise.all does, is just waiting for all the promises to complete. Promise.all所做的,只是等待所有的Promise.all完成。

The line of code that actually executes things is this one:实际执行的代码行是这样的:

const promises = [promise1(), promise2(), promise3()]

Assuming that your promises make HTTP calls:假设您的承诺进行 HTTP 调用:

promise1() is executed -> 1 HTTP call going on promise1()被执行 -> 1 个 HTTP 调用正在进行中
promise2() is executed -> 2 HTTP calls going on执行promise2() -> 2 个 HTTP 调用正在进行
promise3() is executed -> 3 HTTP calls going on promise3()被执行 -> 3 个 HTTP 调用正在进行中

then after a while, in undetermined order, these promises complete.过了一会儿,这些承诺以不确定的顺序完成。

These 3 HTTP calls could complete in the same time, but in your code, you will have 3 sequential completition.这 3 个 HTTP 调用可以同时完成,但在您的代码中,您将有 3 个顺序完成。 For this reason, you can use Promise.all to assure that your callback is executed only when all of your promises complete.出于这个原因,您可以使用Promise.all来确保您的回调仅在您的所有承诺完成时执行。

Remember that there's a limit on the amount of parallel HTTP connections you can have inyour environment, look at this: https://stackoverflow.com/a/985704/7327715请记住,您的环境中可以拥有的并行 HTTP 连接数量是有限制的,请查看: https : //stackoverflow.com/a/985704/7327715

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM