Running independent requests with Promise.all and race
concurrent Promise combinators.
start all requests then await Promise.all to get all results or fail fast on first rejection; use Promise.race when only the fastest settled result matters.
WHAT THIS TESTS This verifies you can choose the right Promise combinator and that you understand requests run in parallel only if you start them before awaiting.
A GOOD ANSWER COVERS To run three independent requests in parallel, begin all of them so they are in flight, then collect their Promises into an array and pass it to Promise.all. Promise.all returns a single Promise that fulfills with an array of every result, in input order, once all inputs fulfill. Crucially it fails fast: if any single input rejects, the combined Promise rejects immediately with that reason, though the other requests still complete in the background. To continue only when the fastest source responds, use Promise.race, which settles with the outcome of whichever input settles first, whether that is a fulfillment or a rejection. If you want the first successful response and want to ignore early failures, Promise.any is the better fit, since it ignores rejections until one fulfills.
COMMON WRONG ANSWERS Awaiting each request sequentially inside a loop, which makes them serial and triples latency. Confusing race with all, or assuming Promise.all somehow cancels the remaining requests when one rejects. Mixing up race, which settles on first settle including rejection, with any, which waits for first fulfillment.
LIKELY FOLLOW-UPS How Promise.allSettled differs when you want all outcomes regardless of failure, the distinction between race and any, whether rejected Promises can be cancelled, and how to add per-request timeouts.
ONE CONCRETE EXAMPLE For a dashboard needing user, billing, and usage data, you start all three fetches, then await Promise.all on the trio so the page renders only when every dataset is ready, and a single failure surfaces an error fast. For a latency-sensitive lookup served by several mirrors, you start a request to each mirror and await Promise.race, taking whichever responds first and discarding the rest.
Read the original → developer.mozilla.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.