Promise.all vs allSettled vs race vs any
All four take an iterable of promises and return one promise. They differ in when that promise settles and what it settles with.
| Method | Fulfils when | Rejects when | Result |
|---|---|---|---|
Promise.all |
All fulfil | Any rejects (immediately) | Array of values, in input order |
Promise.allSettled |
All settle | Never | Array of { status, value } or { status, reason } |
Promise.race |
The first to settle fulfils | The first to settle rejects | That one value or reason |
Promise.any |
The first to fulfil | All reject | That value, or an AggregateError |
1. Promise.all: I need everything
const [user, posts] = await Promise.all([
fetch('/api/user').then((r) => r.json()),
fetch('/api/posts').then((r) => r.json()),
]);
Requests run in parallel and results come back in the order you listed them, not the order they finished. One rejection rejects the whole thing with that reason.
2. Promise.allSettled: tell me how each one went
const results = await Promise.allSettled([loadProfile(), loadAds(), loadFeed()]);
for (const r of results) {
if (r.status === 'fulfilled') render(r.value);
else report(r.reason);
}
Use it when partial success is fine: dashboards with independent widgets, batch uploads, cleanup tasks.
3. Promise.race: first to finish wins, success or failure
function withTimeout(promise, ms) {
const timeout = new Promise((_, reject) =>
setTimeout(() => reject(new Error('Timed out')), ms)
);
return Promise.race([promise, timeout]);
}
If the first promise to settle rejects, race rejects, even if another would have succeeded a moment later.
4. Promise.any: first success wins
const fastest = await Promise.any([
fetch('https://cdn-a.example.com/lib.js'),
fetch('https://cdn-b.example.com/lib.js'),
]);
Rejections are ignored unless every promise rejects. Then you get an AggregateError whose errors array holds each reason.
5. Things that catch people out
- Nothing is cancelled. When
allrejects orracesettles, the other operations keep running. Cancel them yourself with an AbortController. - Empty input.
Promise.all([])fulfils with[].Promise.allSettled([])fulfils with[].Promise.any([])rejects with anAggregateError.Promise.race([])never settles. - Non-promises are fine.
Promise.all([1, somePromise])treats1as already fulfilled. - No concurrency limit.
Promise.all(urls.map(fetch))starts every request at once. For a cap, see map async with limit. awaitin a loop is sequential. Create the promises first, then await them together.
// Sequential: each waits for the previous one
for (const id of ids) results.push(await load(id));
// Parallel
const results = await Promise.all(ids.map(load));
6. Implementing Promise.all
A common interview question.
function promiseAll(iterable) {
return new Promise((resolve, reject) => {
const items = Array.from(iterable);
const results = new Array(items.length);
let remaining = items.length;
if (remaining === 0) return resolve(results);
items.forEach((item, i) => {
Promise.resolve(item).then((value) => {
results[i] = value; // keep input order
if (--remaining === 0) resolve(results);
}, reject); // first rejection wins
});
});
}
Points interviewers look for: results stored by index, non-promise values wrapped with Promise.resolve, the empty case, and rejecting on the first failure. Practise all four in Promise methods.
Choosing a method
| Goal | Use |
|---|---|
| Load several things the page can’t work without | Promise.all |
| Load independent things and handle each outcome | Promise.allSettled |
| Add a timeout, or take whichever settles first | Promise.race |
| Try several sources and use the first that works | Promise.any |
Follow-up questions to expect
- “How would you retry the failed ones?”
allSettled, filter bystatus === 'rejected', retry those. - “How do you avoid an unhandled rejection with
race?” Attach acatchto the losing promises, or cancel them. - “What’s the difference between
raceandany?”racesettles with the first result of either kind;anywaits for the first success.
