Photo by Autumn Mott Rodeheaver on Unsplash

Using AggregateError in JavaScript

See Multiple Errors in One Error

const promises = [
Promise.reject(new Error('failure #1')),
Promise.reject(new Error('failure #2')),
Promise.reject(new Error('failure #3'))
];
Promise.any(promises).then(
(result) => console.log(result),
(error) => console.error(error)
);
// logs:
// => AggregateError: No Promise in Promise.any was resolved
const promises = [
Promise.reject(new Error('failure #1')),
Promise.reject(new Error('failure #2')),
Promise.reject(new Error('failure #3'))
];
Promise.any(promises).catch(error => {
console.log(error.name);
console.log(error.message);
console.log(error.errors);
});
const myError = new AggregateError([
new TypeError('The first argument must be a number'),
new RangeError('The first argument must be between 1 to 10'),
new URIError('The second argument is not a valid URL'),
], 'The operation failed')
try {
throw myError;
} catch (error) {
console.log(error.name);
console.log(error.message);
console.log(error.errors);
}

Faraz is a professional JavaScript developer who is passionate about promoting patterns and ideas that make web development more productive. Website: eloux.com