combinator
Promise.any
Resolves with the value of the first promise to fulfill, ignoring any rejections until a success occurs. If every promise rejects, it rejects with an AggregateError whose .errors array contains each reason.
Promise.any(iterable) Parameters
| Parameter | Purpose |
|---|---|
| iterable | Iterable of promises or values |
| first fulfillment wins | Rejections are ignored while any promise is still pending |
| AggregateError | Thrown when ALL inputs reject; err.errors is the reason array |
Examples
const first = await Promise.any([
fetch('https://a.example/data'),
fetch('https://b.example/data'),
fetch('https://c.example/data')
]); Returns the first mirror that succeeds, tolerating failures from the others.
try {
await Promise.any([Promise.reject('x'), Promise.reject('y')]);
} catch (e) {
console.log(e instanceof AggregateError, e.errors); // true, ['x','y']
} Every promise rejected — you get an AggregateError with all reasons.
await Promise.any([]); // rejects with AggregateError (no promises to succeed) An empty iterable rejects immediately.
Gotcha
Opposite of Promise.race — rejections do NOT short-circuit. ES2021+ only; check runtime support before relying on AggregateError.
Related
Promise.race
Settles as soon as the first promise in the iterable settles, adopting its state (fulfilled or rejected). The remaining promises keep running but their outcomes are ignored.
Promise.all
Waits for all promises in an iterable to fulfill and resolves with an array of their values in original order. Rejects immediately with the reason of the first promise that rejects (fail-fast).
Promise.allSettled
Waits for every promise to settle (fulfill or reject) and always resolves with an array of result descriptors. Each entry is either { status: 'fulfilled', value } or { status: 'rejected', reason }.
← All JS Promise reference · async/await vs promises vs callbacks