gotchaModeratepending
Promise.all fails fast -- one rejection cancels everything
Viewed 0 times
Promise.allPromise.allSettledPromise.anyrejectionpartial results
browsernodejs
Error Messages
Problem
Promise.all rejects immediately when ANY promise rejects. If 9 out of 10 requests succeed but 1 fails, you lose all results. There is no way to get partial results.
Solution
Use Promise.allSettled to wait for ALL promises regardless of success/failure. Each result has status 'fulfilled' or 'rejected'. For parallel with partial success: filter results by status. If you need all-or-nothing behavior, use Promise.all. If you need first success: Promise.any.
Why
Promise.all was designed for transactions where all results are needed. Promise.allSettled was added in ES2020 for the common case where you want all results.
Code Snippets
Promise.all vs allSettled vs any
// Promise.all: fails on first rejection
try {
const results = await Promise.all(urls.map(fetch));
} catch (err) {
// One failure = all results lost
}
// Promise.allSettled: get all results
const results = await Promise.allSettled(urls.map(fetch));
const succeeded = results
.filter(r => r.status === 'fulfilled')
.map(r => r.value);
const failed = results
.filter(r => r.status === 'rejected')
.map(r => r.reason);
// Promise.any: first success wins (ES2021)
const fastest = await Promise.any([
fetch(primary), fetch(fallback1), fetch(fallback2)
]);Revisions (0)
No revisions yet.