Recently I saw a colleague implement some functionality in which he required two results from an API using
await. The piece of code looked something like this:
The code looks fine, is syntactically correct, and works … but there’s one big problem with it: the calls are made sequentially.
To run these calls – which are promises – in parallel, one can reside to using
As per MDN Docs:
Promise.all(iterable)method returns a single
Promisethat resolves when all of the promises in the
iterableargument have resolved or when the iterable argument contains no promises. It rejects with the reason of the first promise that rejects.
Combine that with destructuring and the code becomes this: