Sub topic 13
Choosing the Right Combinator
This chapter assumes you have read the earlier chapters on Promise.all, Promise.allSettled, Promise.race, and Promise.any. You should also be comfortable with async/await and try/catch. All four combinators were covered one at a time. This chapter compares them so you can pick one quickly when you have a real task.
Two Questions Decide the Choice
Every combinator takes an iterable of promises and returns one promise. They differ on two points:
- Do you need every result, or just one?
- What should a rejection do? It can stop everything, be recorded, or be ignored until something succeeds.
Answer those two questions and the combinator follows.
Side-by-Side Comparison
| Combinator | Fulfills when | Rejects when | Fulfillment value | Typical use |
|---|---|---|---|---|
| `Promise.all` | All inputs fulfill | Any input rejects (first one) | Array of values in input order | Loading data that all has to be present |
| `Promise.allSettled` | All inputs settle | Never, apart from errors iterating the input | Array of `{status, value}` / `{status, reason}` | Batch jobs with partial-failure reports |
| `Promise.race` | First input to settle fulfills | First input to settle rejects | That single value | Timeouts |
| `Promise.any` | First input fulfills | All inputs reject | That single value | Fallbacks, redundant mirrors |
Matching Tasks to Combinators
Here are some common situations and the combinator that fits each:
- A page needs the user, their settings, and their permissions. If any of these is missing the page cannot render, so use
Promise.all. - Sending 50 notification emails. Each email is independent, and you want to know which ones failed. Use
Promise.allSettled. - A request must finish within 2 seconds. Race it against a timer that rejects, using
Promise.race. - The same file is on three CDNs. Any copy will do, and an outage at one CDN should not matter. Use
Promise.any.
The example below shows how the choice changes the result for the same inputs:
const ok = (ms, v) => new Promise((r) => setTimeout(() => r(v), ms));
const fail = (ms, msg) => new Promise((_, j) => setTimeout(() => j(new Error(msg)), ms));
async function main() {
const inputs = () => [fail(50, 'primary down'), ok(100, 'backup')];
try { await Promise.all(inputs()); }
catch (e) { console.log('all:', e.message); } // all: primary down
const settled = await Promise.allSettled(inputs());
console.log('allSettled:', settled.map((s) => s.status)); // ['rejected', 'fulfilled']
try { await Promise.race(inputs()); }
catch (e) { console.log('race:', e.message); } // race: primary down
console.log('any:', await Promise.any(inputs())); // any: backup
}
main();inputs() is a function, so each combinator gets fresh promises. A promise settles only once, so the same promises could not be reused across these calls.
Common Mistakes
Promise.allfor independent work. One failed email should not hide the 49 that were sent. If each task stands alone, useallSettled.allSettledwithout checkingstatus. The returned promise always fulfills, so failures pass silently unless you inspect each result.- Forgetting that losers keep running. None of the four combinators cancels the remaining work. The next chapter covers how to cancel it.
- Not choosing a combinator at all. If the tasks don't depend on each other, awaiting them one by one in a loop makes the total time the sum of their durations.
Exercise
You query three regional price services and want the price from whichever answers successfully first. If all three fail, you want to log every error. Which combinator do you use, and where do you find the errors?
Show answer
Use Promise.any. It fulfills with the first successful price and ignores earlier rejections. If all three reject, it rejects with an AggregateError, and its errors array lists each reason in input order.
Exercise
A dashboard loads five independent widgets. A broken widget should show an error tile, and the other widgets should still render. Why is Promise.all the wrong choice, and what should you use instead?
Show answer
Promise.all rejects on the first failure, and you lose the results of the widgets that succeeded. Use Promise.allSettled, then render a widget for each result whose status is 'fulfilled' and an error tile for each result whose status is 'rejected'.