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())

Leave a Comment