Promise.any()
The AggregateError object represents an error when several errors need to be wrapped in a single error. It is thrown when multiple errors need to be reported by an operation, for example by Promise.any(), when all promises passed to it reject.
Compared to SuppressedError, AggregateError represents a list of unrelated errors, while SuppressedError represents an error that happened during the handling of another error.
AggregateError is a subclass of Error.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 85 | 85 | 79 | 14 | 85 | 14 | |
| The AggregateError() constructor creates AggregateError objects. | 85 | 85 | 79 | 14 | 85 | 14 |
| The errors data property of an AggregateError instance contains an array representing the errors that were aggregated. | 85 | 85 | 79 | 14 | 85 | 14 |
| Serializable objects are objects that can be serialized and later deserialized in any JavaScript environment ("realm"). This allows them to, for example, be stored on disk and later restored, or cloned with Window.structuredClone, or shared between workers using DedicatedWorkerGlobalScope.postMessage(). | 98 | 98 | 103 | 17 | 98 | 17 |
| The Promise.any() static method takes an iterable of promises as input and returns a single Promise. This returned promise fulfills when any of the input's promises fulfills, with this first fulfillment value. It rejects when all of the input's promises reject (including when an empty iterable is passed), with an AggregateError containing an array of… | 85 | 85 | 79 | 14 | 85 | 14 |
- `AggregateError` serializes to the `Error` type, without additional properties.
- `AggregateError` serializes to the `Error` type, without additional properties.
- `AggregateError` serializes to the `AggregateError` type, with properties `name`, `message`, `cause`, and `errors`.
- `AggregateError` serializes to the `Error` type, without additional properties.
Syntax
// Fetch the resource from the fastest CDN
const resource = await Promise.any([
fetch('https://cdn1.example.com/data'),
fetch('https://cdn2.example.com/data'),
fetch('https://cdn3.example.com/data'),
]);
console.log(resource); // The first successful response Live demo
Use cases
-
Fastest available source
Race multiple mirrors or replicated services and keep whichever one succeeds first.
-
Graceful fallback chains
Try several providers at once when a task should continue as long as any one source can answer.
Cautions
- If every Promise rejects, Promise.any throws AggregateError, so error handling must account for the combined failure.
- Use it only when any successful response is acceptable, not when every result must be inspected.
Accessibility
- When users may notice source switching or fallback behavior, surface a stable status message instead of changing content silently.