Sub topic 6
Promise.allSettled: Collect Every Outcome
This chapter assumes you know how to create and await promises, how try/catch works with async functions, and how Promise.all behaves, including its fail-fast rejection. The examples use top-level await, so run them as an ES module, for example in a .mjs file with Node.js 14.8 or later.
Why another combinator?
Promise.all answers one question: did everything succeed? If any input rejects, you get only that first rejection reason. The values from the inputs that succeeded are lost to you, even though those operations finished.
Often you need to know what happened to each task. Examples include sending notifications to many users, refreshing several caches, or calling a list of health-check endpoints. One failure should not hide the other results. Promise.allSettled covers this case. It waits for every input to settle, either fulfilled or rejected, and then fulfills with a description of each outcome.
How it settles
Promise.allSettled takes an iterable and returns one promise. That promise has these properties:
- It waits until every input has settled. It never short-circuits.
- It fulfills with an array that has one entry per input, in input order.
- It never rejects because an input rejected. A rejection becomes an entry in the array.
A worked example
const outcomes = await Promise.allSettled([
Promise.resolve('ok'),
Promise.reject(new Error('failed'))
]);
for (const outcome of outcomes) {
if (outcome.status === 'fulfilled') {
console.log('value:', outcome.value);
} else {
console.log('reason:', outcome.reason.message);
}
}
// value: ok
// reason: failedEach entry is a plain object. A fulfilled input produces { status: 'fulfilled', value }. A rejected input produces { status: 'rejected', reason }. You do not need a try/catch around this await, because the returned promise does not reject when an input rejects.
The next chapter covers how to read these objects and build a partial-failure report.
Compared with Promise.all
| Behavior | `Promise.all` | `Promise.allSettled` |
|---|---|---|
| Waits for | All to fulfill, or the first rejection | All to settle |
| Fulfillment value | Array of values | Array of `{ status, value / reason }` objects |
| Rejects when | Any input rejects | Never, because of an input |
| Typical use | Every result is required | Each result is useful on its own |
Common errors
- Treating the result as values. Writing
const [user, posts] = await Promise.allSettled(...)gives you result objects. To get the data, useuser.value. - Waiting too long. allSettled waits for the slowest input. If one request hangs, the whole call hangs. Pair slow inputs with a timeout, which is covered in a later chapter.
- Using it when one failure should stop the work. If the results only make sense together,
Promise.allfails sooner and its intent is clearer.
Exercise
What does this code log, and does the await ever throw? const r = await Promise.allSettled([Promise.reject(new Error('x')), 42]); console.log(r.map(o => o.status));
Show answer
It logs ['rejected', 'fulfilled']. The plain value 42 is treated as an already-fulfilled promise. The await does not throw, because allSettled fulfills even when inputs reject. The error 'x' is available as r[0].reason.
Exercise
You fetch a profile and a list of recommendations. The page cannot render without the profile, but it can render without recommendations. Which combinator fits better, and why?
Show answer
Use Promise.allSettled, then check the profile's status yourself and throw if it was rejected. This keeps recommendations optional. With Promise.all, a failed recommendations request would reject the whole call and block rendering for no good reason.