AggregateError
Several errors in one — thrown by Promise.any.
Available since: ES2021
Syntax
new AggregateError(errors, message)Parameters
| Parameter | Type | Description |
|---|---|---|
| errors | Iterable | A list of errors. |
| message | string | Optional. A general description. |
Return value
AggregateErrorAn error with an errors property — an array of the inner errors.
Examples
Every promise rejected:
try {
await Promise.any([
Promise.reject(new Error('Server A down')),
Promise.reject(new Error('Server B down')),
]);
} catch (error) {
console.log(error.name);
console.log(error.errors.map((e) => e.message).join(', '));
}Output:
AggregateError
Server A down, Server B downNotes
It is handy for your own validation too: collect every form error and throw them together as one.
See also
- Promise.any — Returns the first successful result and skips errors.
- Error — The base error class: created and thrown with throw.
In the tutorial
- Several promises at once — Promise.all, Promise.allSettled, Promise.race and Promise.any in JavaScript: how to wait for several asynchronous actions at the same time.