Choose Promise.all or allSettled
Use Promise.all when one failure should fail the whole step and allSettled when partial results are fine, then read each result by its status.
A page needs a profile, billing details and an avatar. You load them with Promise.all, billing is down, and the page shows an error screen even though two of the three requests worked.
Promise.all is all or nothing
Promise.all takes the first rejection as the answer for the whole group.
const wait = (ms, label, fail = false) =>
new Promise((resolve, reject) =>
setTimeout(() => {
console.log(`${label} finished`)
fail ? reject(new Error(`${label} failed`)) : resolve(label)
}, ms),
)
try {
const results = await Promise.all([
wait(50, 'profile'),
wait(100, 'billing', true),
wait(200, 'avatar'),
])
console.log(results)
} catch (error) {
console.log('caught:', error.message)
}
await wait(300, 'done waiting')
// profile finished
// billing finished
// caught: billing failed
// avatar finished
// done waiting finishedThe catch block ran as soon as billing failed, and results was never assigned. The avatar request kept running and finished after the catch, because nothing cancels a promise that is already in flight.
Use this when the next step needs all the results, such as a checkout that needs both the cart and its current price.
allSettled keeps every outcome
Promise.allSettled waits for every promise and reports each one. With the same wait helper, it resolves with an array even though billing rejects.
const results = await Promise.allSettled([
wait(50, 'profile'),
wait(100, 'billing', true),
wait(200, 'avatar'),
])
console.log(results)
// profile finished
// billing finished
// avatar finished
// [
// { status: 'fulfilled', value: 'profile' },
// {
// status: 'rejected',
// reason: Error: billing failed
// at Timeout._onTimeout (...)
// },
// { status: 'fulfilled', value: 'avatar' }
// ]The result keeps the input order, and each entry has a status plus either a value or a reason. I ask what the caller does when one piece is missing. If it can draw the page with a gap, I use allSettled and treat each rejected entry as the gap.
Read the result by its status
In TypeScript you cannot read value straight off an entry. The compiler reports Property 'value' does not exist on type 'PromiseSettledResult<{ name: string; }>', because a rejected entry has a reason instead.
async function loadPart(name: string): Promise<{ name: string }> {
if (name === 'billing') throw new Error('billing failed')
return { name }
}
const results = await Promise.allSettled(['profile', 'billing', 'avatar'].map(loadPart))
for (const result of results) {
if (result.status === 'fulfilled') {
console.log('ok', result.value.name)
} else {
console.log('failed', result.reason instanceof Error ? result.reason.message : String(result.reason))
}
}
// ok profile
// failed billing failed
// ok avatarChecking status narrows each entry. Inside the if, value is typed as the resolved object. The else branch gives you reason. A promise can reject with any value, so check for an Error before reading its message.