I would like to clarify this point, as the documentation is not too clear about it;
Q1: Is Promise.all(iterable)
processing all promises sequentially or in parallel? Or, more specifically, is it the equivalent of running chained promises like
p1.then(p2).then(p3).then(p4).then(p5)....
or is it some other kind of algorithm where all p1
, p2
, p3
, p4
, p5
, etc. are being called at the same time (in parallel) and results are returned as soon as all resolve (or one rejects)?
Q2: If Promise.all
runs in parallel, is there a convenient way to run an iterable sequencially?
Note: I don't want to use Q, or Bluebird, but all native ES6 specs.
just to elaborate on @Bergi's answer (which is very succinct, but tricky to understand ;)
This code will run each item in the array and add the next 'then chain' to the end;
hope that makes sense.
You can also process an iterable sequentially with an async function using a recursive function. For example, given an array
a
to process with asynchronous functionsomeAsyncFunction()
: