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?
2 Answers
Reset to default 11Javascript 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
async
function with--target ES2017
or higher and see change detection fail miserably. – Aluan Haddad Commented Sep 4, 2020 at 21:11Promise.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