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.
In parallel
Advantages: Faster. All iterations will be executed even if one fails.
In sequence
Advantages: Variables in the loop can be shared by each iteration. Behaves like normal imperative synchronous code.
You can do it by for loop.
async function return promise
if you write following code then client are created parallelly
then all clients are created parallelly. but if you want to create client sequentially then you should use for loop
then all clients are created sequentially.
happy coding :)
I've been using for of in order to solve sequential promises. I'm not sure if it helps here but this is what I've been doing.
this might answer part of your question.
yes, you can chain an array of promise returning functions as follows... (this passes the result of each function to the next). you could of course edit it to pass the same argument (or no arguments) to each function.
Bergis answer got me on the right track using Array.reduce.
However, to actually get the functions returning my promises to execute one after another I had to add some more nesting.
My real use case is an array of files that I need to transfer in order one after another due to limits downstream...
Here is what I ended up with.
As previous answers suggest, using:
didn't wait for the transfer to complete before starting another and also the "All files transferred" text came before even the first file transfer was started.
Not sure what I did wrong, but wanted to share what worked for me.
Edit: Since I wrote this post I now understand why the first version didn't work. then() expects a function returning a promise. So, you should pass in the function name without parentheses! Now, my function wants an argument so then I need to wrap in in a anonymous function taking no argument!
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.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 fromto
and the solution to do that is using
Array::reduce
: