最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Does Promise.all() run in sequential or parallel? - Stack Overflow

programmeradmin0浏览0评论

Does Promise.all() run in sequential or parallel in 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? I would assume like Node, Javascript in the browser to be single threaded thus they don't run in parallel?

Does Promise.all() run in sequential or parallel in 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? I would assume like Node, Javascript in the browser to be single threaded thus they don't run in parallel?

Share Improve this question edited Aug 23, 2021 at 15:58 Liam 29.7k28 gold badges137 silver badges200 bronze badges asked Sep 4, 2020 at 19:49 emarelemarel 4017 silver badges31 bronze badges 6
  • 3 No, they are asynchronous. Single threaded does not mean they run synchronously or in parallel. A single thread can still time share. – Taplar Commented Sep 4, 2020 at 19:53
  • 2 Node is single threaded in user code. If you call two promises that read files, they can be read in the same time. Continuations (results) are again single threaded. – Wiktor Zychla Commented Sep 4, 2020 at 19:54
  • 2 Angular does not (or should not) affect the Promise runtime semantics – Mulan Commented Sep 4, 2020 at 19:56
  • @Thankyou, that's true but I find it hard to trust zone.js. Angular relies on several additions to native Promises provided by zone.js. Try writing an async function with --target ES2017 or higher and see change detection fail miserably. – Aluan Haddad Commented Sep 4, 2020 at 21:11
  • Neither! Promise.all() doesn't run anything. It aggregates Promises, where each Promise is an abstraction of a value. – Roamer-1888 Commented Sep 5, 2020 at 17:13
 |  Show 1 more comment

2 Answers 2

Reset to default 11

Javascript is a single threaded application. However, asynchronous calls allow you to not be blocked by that call. This is particularly useful when making REST API calls. 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(). 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. 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. And there is a chance that promise2() gets resolved before promise1() and so on. The function Promise.all() waits for all the promises provided to it to fulfill or at least one of them to fail.

Learn more about Javascript event loops in this video by Jake Archibald.

Promise.all does not make your promises run in parallel.
Promise.all does not make your promises run at all.
What Promise.all does, is just waiting for all the promises to complete.

The line of code that actually executes things is this one:

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

Assuming that your promises make HTTP calls:

promise1() is executed -> 1 HTTP call going on
promise2() is executed -> 2 HTTP calls going on
promise3() is executed -> 3 HTTP calls going on

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. For this reason, you can use Promise.all to assure that your callback is executed only when all of your promises complete.

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

发布评论

评论列表(0)

  1. 暂无评论