🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
JS MASTER CLASS /// MASTER THE ENGINE /// BUILD LOGIC /// ASYNC PATTERNS /// JS MASTER CLASS /// MASTER THE ENGINE ///

Promise.any() Deep Dive | JavaScript Tutorial - In-Depth Guide

Go deeper on Promise.any(): its ignore-rejections-until-all-fail behavior, the AggregateError it throws, and redundant-source fallback patterns it enables.

Total XP: 0|💻 javascript XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

System Hub

Core logic.

Quick Quiz //

If the first promise passed to Promise.any() rejects but the second one later succeeds, what does any() resolve with?


🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.

Promise.any() resolves with the first promise to succeed, ignoring rejections entirely — only failing if every single promise rejects. It is the natural fit for redundant, fallback-style requests.

1Promise.any() Deep Dive | JavaScript Tutorial - In-Depth Guide Part 1

Promise.any() resolves as soon as any one promise fulfills, completely ignoring earlier rejections along the way.

+
const result = await Promise.any([
  Promise.reject('fails fast'),
  delay(100).then(() => 'succeeds slower'),
]);
// 'succeeds slower' — the rejection is simply ignored
localhost:3000
🏆

Ignores Rejections

2Promise.any() Deep Dive | JavaScript Tutorial - In-Depth Guide Part 2

Promise.any() only rejects if every single input promise rejects — and when it does, it throws a special AggregateError containing all the individual rejection reasons.

+
try {
  await Promise.any([Promise.reject('A'), Promise.reject('B')]);
} catch (err) {
  err instanceof AggregateError; // true
  err.errors; // ['A', 'B']
}
localhost:3000

AggregateError on Total Failure

3Promise.any() Deep Dive | JavaScript Tutorial - In-Depth Guide Part 3

The classic use case is querying multiple redundant sources for the same data and accepting whichever one responds successfully first.

+
const data = await Promise.any([
  fetch('https://mirror1.example.com/data'),
  fetch('https://mirror2.example.com/data'),
  fetch('https://mirror3.example.com/data'),
]);
localhost:3000

Redundant Source Fallback

4Promise.any() Deep Dive | JavaScript Tutorial - In-Depth Guide Part 4

Like race() and all(), any() does not cancel the other still-pending promises once it resolves — they keep running in the background.

+
// Other mirror requests keep running even after one succeeds:
const data = await Promise.any(mirrorRequests);
// Consider AbortController to cancel the rest here
localhost:3000

Non-Winners Keep Running

5Promise.any() Deep Dive | JavaScript Tutorial - In-Depth Guide Part 5

Promise.any() on an empty array immediately rejects with an AggregateError — there are no promises that could possibly succeed.

+
await Promise.any([]); // immediately rejects with AggregateError
localhost:3000

Empty Array Rejects Immediately

6Step-by-Step Breakdown

Promise.any() resolves as soon as any one promise fulfills, completely ignoring earlier rejections along the way.

Checkpoint: If the first promise passed to Promise.any() rejects but the second one later succeeds, what does any() resolve with?

  • The second promise's successful value
  • It rejects immediately with the first error

Promise.any() only rejects if every single input promise rejects — and when it does, it throws a special AggregateError containing all the individual rejection reasons.

Checkpoint: What type of error does Promise.any() throw when every input promise rejects?

  • AggregateError, containing every rejection reason
  • A generic TypeError

The classic use case is querying multiple redundant sources for the same data and accepting whichever one responds successfully first.

Like race() and all(), any() does not cancel the other still-pending promises once it resolves — they keep running in the background.

Promise.any() on an empty array immediately rejects with an AggregateError — there are no promises that could possibly succeed.

Next, we'll explore 'AbortController'.

Level Up 🚀

Advanced cheat sheets, SEO tricks, and interview prep for this topic.

Browser Support

ChromeSupported

Fully supported.

FirefoxSupported

Fully supported.

SafariSupported

Fully supported.

EdgeSupported

Fully supported.

Accessibility (A11y)

1Report Total Failure Clearly When Every Redundant Source Fails

If Promise.any() rejects because every mirrored data source failed, ensure the resulting user-facing error message is announced clearly via accessible means, rather than showing a blank or broken state with no explanation.

SEO Implications

  • 1

    Fallback Sourcing Improves Content Reliability

    Using Promise.any() to fall back across multiple redundant data or asset sources reduces the odds of a page failing to render its content entirely due to one source being temporarily unavailable, supporting more consistent crawlability.

Best Practices

Use Promise.any() for Genuinely Redundant/Equivalent Sources

It's the right tool specifically when any one successful result is equally acceptable — using it for operations with meaningfully different results per source would silently discard useful information about which source actually answered.

Inspect AggregateError.errors When Handling Total Failure

Logging just the AggregateError itself hides which individual sources failed and why; iterate `.errors` to capture the full diagnostic picture.

Frequent Bugs

THE BUG

Catching a Promise.any() rejection and logging `err.message`, which is unhelpfully generic, instead of inspecting `err.errors` for the actual individual failure reasons.

THE FIX

Iterate over `err.errors` (available on the thrown AggregateError) to see and log every individual rejection reason.

THE BUG

Confusing Promise.any() with Promise.race(), expecting the fastest promise (success or failure) to win, when any() specifically waits for a success.

THE FIX

Remember any() only cares about the first success — a fast rejection is simply ignored and waiting continues for a later success, unlike race() which reacts to whichever settles first regardless of outcome.

Real-World Examples

Querying Multiple CDN Mirrors for the Fastest Available Copy

A large asset needed to be fetched from whichever of several geographically distributed CDN mirrors responded successfully first, tolerating any individual mirror being down.

async function fetchFromFastestMirror(mirrors) {
  return Promise.any(mirrors.map((url) => fetch(url)));
}

Interview Prep

?Frequently Asked Questions

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Logging a generic AggregateError message instead of its individual errors

catch (err) { err.errors.forEach(e => console.error(e)); }

The Solution //

Iterate `err.errors` to see and report every individual rejection reason.

Lesson Glossary

[01]Promise.any()

Resolves with the first fulfilled promise, ignoring rejections unless all promises reject.

Code Preview
Promise.any([p1,p2])

[02]AggregateError

An error type wrapping multiple individual errors, thrown by Promise.any() when every promise rejects.

Code Preview
err.errors

[03]Redundant Source Pattern

Querying multiple equivalent sources and accepting whichever responds first successfully.

Code Preview
mirrored requests

[04]Rejection Tolerance

any()'s behavior of ignoring failures as long as at least one promise ultimately succeeds.

Code Preview
ignores early failures

[05]Total Failure

The scenario where every single input promise rejects, triggering an AggregateError.

Code Preview
all failed

Continue Learning