Course contentsShow
← JavaScript
Lesson 1974 of 2,72341. Modern JavaScript Features (ES2020+)Free lesson

Promise.any vs Promise.race

Promise.race settles on first completion; Promise.any waits for first success, handling failures differently.

Promise.any vs Promise.race

You'll learn: How Promise.race and Promise.any differ in handling multiple promises, especially when some fail.

The Core Difference

Both methods accept an array of promises and return a single promise, but their settlement logic is fundamentally different:

Promise.race settles (resolves or rejects) as soon as any promise completes first, regardless of whether it succeeded or failed. It's a true race—first across the finish line wins, whether that's success or failure.

Promise.any waits for the first successful resolution. If a promise rejects, it keeps waiting for others. Only if all promises reject does Promise.any itself reject with an AggregateError.

The Racing Analogy

Think of a race where you need at least one winner:

  • Promise.race: The race ends the instant the first runner crosses the finish line or falls down. You get that runner's result immediately, success or failure.
  • Promise.any: You wait until someone successfully finishes. Runners who fall are ignored unless everyone falls—only then do you declare the race a failure.

When to Use Which

// Promise.race: whoever finishes first
const first = Promise.race([slowAPI, fastAPI]);
// If fastAPI rejects first, you get that rejection

// Promise.any: first success, ignoring failures
const firstSuccess = Promise.any([flakeyAPI1, flakeyAPI2, flakeyAPI3]);
// If flakeyAPI1 fails, it keeps trying others
// Only rejects if ALL fail (returns AggregateError)

Use Promise.race when you care about the first completion (timeout patterns, speed comparisons).

Use Promise.any when you want the first success and can tolerate some failures (redundant API calls, fallback strategies).

Key Takeaway: Promise.race settles on the first completion (success or failure), while Promise.any waits for the first success, only rejecting if every promise fails.