Facebook Pixel

Promise.all vs allSettled vs race vs any in JavaScript

Four promise combinators with different behaviors. Here is each one and when to use it.

Promise.all vs allSettled vs race vs any in JavaScript

JavaScript has four promise combinators. Each has a different behavior. Here is each one and when to use it.

Promise.all (All Must Succeed)

const [a, b, c] = await Promise.all([fetchA(), fetchB(), fetchC()]); `` - Runs all promises in parallel. - Waits for all to fulfill. - If any rejects, the whole thing rejects (fast fail). - Results are in order. - **Use for**: when you need all results and any failure is a total failure. ### Promise.allSettled (Wait for All, Even Failures) ```js const results = await Promise.allSettled([fetchA(), fetchB(), fetchC()]); results.forEach((r) => { if (r.status === "fulfilled") console.log(r.value); else console.error(r.reason); }); `` - Runs all promises in parallel. - Waits for all to settle (fulfilled or rejected). - Never rejects. Returns an array of `{ status, value/reason }`. - **Use for**: when you want all results regardless of failures (partial success). ### Promise.race (First to Settle Wins) ```js const result = await Promise.race([fetchA(), fetchB()]); `` - Runs all promises in parallel. - Returns as soon as the first promise settles (fulfilled or rejected). - If the first to settle fulfills, the race fulfills with its value. - If the first to settle rejects, the race rejects with its reason. - **Use for**: timeouts, first successful result from multiple sources. ### Promise.any (First Success Wins) ```js const result = await Promise.any([fetchA(), fetchB(), fetchC()]); `` - Runs all promises in parallel. - Returns as soon as the first promise **fulfills**. - If all promises reject, it rejects with `AggregateError` (all rejection reasons). - **Use for**: trying multiple sources and taking the first successful one. ### Comparison | Combinator | Waits For | Rejects If | Returns | |---|---|---|---| | `Promise.all` | All fulfill | Any rejects | Array of values | | `Promise.allSettled` | All settle | Never | Array of { status, value/reason } | | `Promise.race` | First settles | First rejects | First value or error | | `Promise.any` | First fulfills | All reject | First value (or AggregateError) | ### Example: Timeout with Promise.race ```js function withTimeout(promise, ms) { const timeout = new Promise((_, reject) => setTimeout(() => reject(new Error("timeout")), ms) ); return Promise.race([promise, timeout]); } const result = await withTimeout(fetch("/api"), 5000); `` ### The Takeaway `Promise.all` (all must succeed, fast fail), `Promise.allSettled` (all settle, partial success), `Promise.race` (first to settle, timeouts), `Promise.any` (first success, all-fail returns `AggregateError`). Use the right combinator for your use case.

Promise.all waits for all and rejects if any rejects (fast fail). allSettled waits for all and never rejects (returns status objects). race returns the first to settle (fulfilled or rejected). any returns the first to fulfill (rejects with AggregateError if all reject).

When you want all results even if some promises reject (partial success). Promise.all rejects if any promise rejects. allSettled waits for all and returns an array of { status, value/reason } objects, so you can handle each result individually.

When you want the first promise to settle (fulfilled or rejected). Common use: implementing a timeout. Race the operation against a timeout promise; whichever settles first wins.

Promise.any returns the first promise to fulfill. If all promises reject, it rejects with an AggregateError containing all rejection reasons. Use it when you want the first successful result from multiple sources (e.g., multiple API endpoints).

An error type thrown by Promise.any when all promises reject. It has an errors property containing an array of all the individual rejection reasons. This lets you see why each promise failed.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.