Are there any performance gains of resolving Promises via Promise.all()
, compared to doing so sequentially?
Example:
// I understand that this always resolves sequentially via blocking.
await promise1;
await promise2;
// Here promises can resolve in any order. Is this any better/faster than the above?
await Promise.all([promise1, promise2])
Trying to understand what's the best approach for concurrency in JS. Thanks in advance.
EDIT:Duplicate of this.
My take-aways:
- key advantage is the fail-fast behavior of Promise.all(). In that regards, there is a performance benefit - however it's negligible. The other is better error handling.
- always use
Promise.all()
for scheduling multiple concurrent operations.