What does Promise.any() do?
What Promise.any() does
Promise.any(iterable) takes an array (or any iterable) of promises
and returns a new promise that:
- fulfills, as soon as the first promise resolves successfully;
- rejects, if all the promises are rejected.
In other words: "give me the first successful result - ignore the errors".
Syntax
Promise.any(iterable)Returns:
a new Promise that:
- fulfills with the value of the first
fulfilledpromise; - rejects with a special
AggregateErrorif all the promises fail.
Example 1 - the first success "wins"
const p1 = Promise.reject('Error 1');
const p2 = new Promise(resolve => setTimeout(() => resolve('Success 2'), 1000));
const p3 = new Promise(resolve => setTimeout(() => resolve('Success 3'), 2000));
Promise.any([p1, p2, p3])
.then(result => console.log('Result:', result))
.catch(err => console.error('Error:', err));Output after 1 second:
Result: Success 2The first success (p2) "wins",
even if the others are still running or fail later.
Example 2 - if all promises reject
const p1 = Promise.reject('Error A');
const p2 = Promise.reject('Error B');
Promise.any([p1, p2])
.then(console.log)
.catch(err => console.error(err));Output:
AggregateError: All promises were rejectedThe error contains a list of all reasons:
err.errors // ['Error A', 'Error B']How it works under the hood
- All promises run in parallel.
- If at least one of them fulfills successfully (
resolve) →Promise.any()immediately resolves with that result. - If all of them turn out
rejected→ it returns an error of typeAggregateErrorwith an array of all reasons.
Example 3 - a mix of successes and errors
const slow = new Promise(resolve => setTimeout(() => resolve('Slow success'), 2000));
const fail = Promise.reject('Error!');
const fast = new Promise(resolve => setTimeout(() => resolve('Fast success'), 500));
Promise.any([slow, fail, fast])
.then(console.log)
.catch(console.error);Result after 0.5 sec:
Fast successEven though fail fails earlier, Promise.any() waits for the first successful promise.
Difference from other methods
| Method | What it returns | Resolves when | Error when |
|---|---|---|---|
Promise.all() | An array of all successful results | All promises are fulfilled | The first reject |
Promise.allSettled() | An array of statuses (fulfilled / rejected) | All promises have settled | Never |
Promise.race() | The first promise to settle (success or error) | Any resolve or reject | Resolves on the first reject |
Promise.any() | The first successful result | The first fulfilled | When all are rejected |
In other words:
Promise.race()reacts to the first promise to settle (including errors),Promise.any()reacts only to the first success.
Example 4 - "backup" (fallback)
Imagine you have three servers: you want to get data from the first one that responds successfully, even if the other two are unavailable.
const fetchFromA = fetch('/api/a'); // can fail
const fetchFromB = fetch('/api/b');
const fetchFromC = fetch('/api/c');
Promise.any([fetchFromA, fetchFromB, fetchFromC])
.then(res => console.log('Response received'))
.catch(() => console.error('All servers unavailable'));This is how you implement a "fallback" strategy - success from any source is considered enough.
Example 5 - AggregateError behavior
Promise.any([
Promise.reject('Error 1'),
Promise.reject('Error 2')
]).catch(err => {
console.error(err.name); // AggregateError
console.error(err.errors); // ['Error 1', 'Error 2']
});A simple analogy
Imagine you send three couriers: a bike, a car, and a rocket.
Promise.any()says: "whoever delivers successfully first, I take their result; the errors of the rest - don't matter".- Only if all of them get lost - only then do you learn about the error.
SUMMARY
| Property | Value |
|---|---|
| What it does | Returns the first successfully fulfilled promise |
| If all are rejected | Returns AggregateError with the reasons |
| Return type | Promise |
| Similar to | Promise.race(), but ignores reject |
| Typical scenarios | fallback requests, backup data sources, redundant API requests |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.