AggregateError
In one line
AggregateError is an Error subclass that holds an array of multiple errors, used by Promise.any when all promises reject.
In simple words
AggregateError is a built-in Error subclass that bundles multiple errors into one object. Its errors property is an array of the underlying errors. It is returned by Promise.any when every input promise rejects — since Promise.any expects at least one success, all-failing is an aggregate failure.
Before AggregateError, there was no standard way to represent "multiple things went wrong". Libraries invented their own multi-error types. AggregateError standardizes this: you can also construct it directly with new AggregateError(errors, message) when you need to throw several errors at once.
Use it when validating multiple fields (collect all errors and throw one AggregateError), when running parallel operations that may all fail, and when you want to report every problem rather than just the first. The errors array lets the caller inspect each individual failure.
Example
// Promise.any rejects with AggregateError if ALL promises reject
async function fetchFirst(urls) {
try {
return await Promise.any(urls.map(u => fetch(u).then(r => r.json())));
} catch (e) {
if (e instanceof AggregateError) {
console.log(`All ${e.errors.length} sources failed:`);
e.errors.forEach((err, i) => console.log(` ${i}:`, err.message));
}
throw e;
}
}
// fetchFirst(["/a", "/b", "/c"]) — uses first that succeeds, or AggregateErrorPromise.any returns the first successful result. If all reject, it throws an AggregateError whose .errors array holds each rejection.
Related terms
Related Resources
Related Lessons
- Errors
Learn AggregateError.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.