Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

AggregateError

Several errors in one — thrown by Promise.any.

Available since: ES2021

Syntax

new AggregateError(errors, message)

Parameters

ParameterTypeDescription
errorsIterableA list of errors.
messagestringOptional. 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 down

Notes

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.

Thanks for reading!

Donate to the site — we work hard for you.