As far as I understand, in ES7/ES2016 putting multiple await
's in code will work similar to chaining .then()
with promises, meaning that they will execute one after the other rather than in parallel. So, for example, we have this code:
await someCall();
await anotherCall();
Do I understand it correctly that anotherCall()
will be called only when someCall()
is completed? What is the most elegant way of calling them in parallel?
I want to use it in Node, so maybe there's a solution with async library?
EDIT: I'm not satisfied with the solution provided in this question: Slowdown due to non-parallel awaiting of promises in async generators, because it uses generators and I'm asking about a more general use case.
Best Answer
You can await on
Promise.all()
:To store the results:
Note that
Promise.all
fails fast, which means that as soon as one of the promises supplied to it rejects, then the entire thing rejects.If, instead, you want to wait for all the promises to either fulfill or reject, then you can use
Promise.allSettled
. Note that Internet Explorer does not natively support this method.