Is Node.js native Promise.all processing in parallel or sequentially? Is Node.js native Promise.all processing in parallel or sequentially? javascript javascript

Is Node.js native Promise.all processing in parallel or sequentially?


Is Promise.all(iterable) executing all promises?

No, promises cannot "be executed". They start their task when they are being created - they represent the results only - and you are executing everything in parallel even before passing them to Promise.all.

Promise.all does only await multiple promises. It doesn't care in what order they resolve, or whether the computations are running in parallel.

is there a convenient way to run an iterable sequencially?

If you already have your promises, you can't do much but Promise.all([p1, p2, p3, …]) (which does not have a notion of sequence). But if you do have an iterable of asynchronous functions, you can indeed run them sequentially. Basically you need to get from

[fn1, fn2, fn3, …]

to

fn1().then(fn2).then(fn3).then(…)

and the solution to do that is using Array::reduce:

iterable.reduce((p, fn) => p.then(fn), Promise.resolve())


In parallel

await Promise.all(items.map(async (item) => {   await fetchItem(item) }))

Advantages: Faster. All iterations will be started even if one fails later on.

In sequence

for (let i = 0; i < items.length; i++) {  await fetchItem(items[i])}

Advantages: Variables in the loop can be shared by each iteration. Behaves like normal imperative synchronous code.


NodeJS does not run promises in parallel, it runs them concurrently since it’s a single threaded event loop architecture. There is a possibility to run things in parallel by creating a new child process to take advantage of the multiple core CPU.

Parallel Vs Concurent

In fact, what Promise.all does is, stacking the promises function in the appropriate queue (see event loop architecture) running them concurrently (call P1, P2,...) then waiting for each result, then resolving the Promise.all with all the promises results. Promise.all will fail at the first promise which fail, unless you have manage the rejection yourself.

There is a major difference between parallel and concurrent, the first one will run different computation in separate process at exactly the same time and they will progress at there rythme, while the other one will execute the different computation one after another without waiting for the previous computation to finish and progress at the same time without depending on each other.

Finally, to answer your question, Promise.all will not execute neither in parallel or sequentially but concurrently.