How can I avoid awaiting independent work one request at a time?
Quick answer
If two asynchronous operations do not depend on each other, start both before waiting for their results, then await Promise.all with their promises. This lets the operations proceed concurrently, and the results come back in the same order as the promises in the array. Keep awaits sequential when the second operation needs a value produced by the first.
The question, the details, the next step.
A practical checklist
List the asynchronous operations and the inputs each one needs.
Mark whether any operation needs a value returned by another.
Start independent operations before awaiting their combined results.
Keep dependent steps sequential and pass the earlier result explicitly.
Check that the
Promise.allresult order matches the variables receiving it.Decide how the surrounding code should handle a rejected promise.
A few more useful answers
Does `Promise.all` start the operations for me?
Call each operation to obtain its promise, then pass those promises to Promise.all. Calling the functions obtains the promises; Promise.all waits for them together. [S1]
Does `Promise.all` return results in the order operations finish?
No. Its result order matches the order of the promises in the input array, not their completion order. [S1]
Should I use `Promise.all` if one operation depends on another?
Not for that dependency: await the first result, then use it to start the next operation. Use Promise.all for operations that can be started independently.
Sources
References used for this guide.
- [S1] Promise.all() — JavaScript | MDN developer.mozilla.org · Checked
