Promise.any()
The redundancy combinator. Where race settles on the first thing to finish, any waits for something to actually work — which is what you almost always mean when you have several equivalent sources.
Demo
Each input is [outcome, value, delay in ms]: 'ok' fulfils with the value, anything else rejects with an Error carrying it. The first case is the contrast with race: the rejection at 10ms does not end the wait, and the success at 30ms wins. Only when EVERY input rejects does any reject, with an AggregateError whose own message is generic — the real reasons are in its errors array. An empty list rejects immediately, which is the sensible opposite of Promise.race([]), which hangs forever.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| iterable | iterable | yes | Any iterable of promises. An empty iterable rejects immediately with an AggregateError — unlike race, which would hang. |
Return value
Promise — A Promise for the first FULFILLED result. Rejects only if every input rejects, with an AggregateError whose errors property holds all the reasons.
Common patterns
const data = await Promise.any(mirrors.map(u => fetch(u)));
try { await Promise.any(tasks); } catch (e) { console.error(e.errors); }
await Promise.race([work, timeout(5000)]);
Examples
Pitfalls
catch (e) { log(e.message); }
catch (e) { log(e.errors.map(x => x.message)); }
await Promise.any([brokenMirror, goodMirror])
const rs = await Promise.allSettled([brokenMirror, goodMirror]);
await Promise.any([slowWork, timeout(100)])
await Promise.race([slowWork, timeout(100)])
Promise.any(tasks)
Promise.all(tasks.map(p => p.then(v => Promise.reject(v), e => e))) .then(errs => Promise.reject(errs), v => v)
When to use
- Several equivalent sources — mirrors, replicas, CDNs
- Fallback chains where any one success is enough
- Racing a cache against a network fetch
- Timeouts → Promise.race, which respects rejections
- You need every result → Promise.all or allSettled
- You need to know about the failures that were tolerated → allSettled
- Targeting runtimes older than 2020 → the inverted-all idiom
Notes
FAQ
any for redundancy — first thing that WORKS. race for timeouts — first thing that FINISHES. The distinction only shows up when something fails quickly, which is exactly the case you are guarding against.