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 ignoredIgnores 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']
}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'),
]);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 hereNon-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 AggregateErrorEmpty 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
Fully supported.
Fully supported.
Fully supported.
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
Catching a Promise.any() rejection and logging `err.message`, which is unhelpfully generic, instead of inspecting `err.errors` for the actual individual failure reasons.
Iterate over `err.errors` (available on the thrown AggregateError) to see and log every individual rejection reason.
Confusing Promise.any() with Promise.race(), expecting the fastest promise (success or failure) to win, when any() specifically waits for a success.
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)));
}