414 words
2 minutes

Promise.any AggregateError: Match Errors to Their Inputs

2026-11-22
Frontend
JavaScript
/
Promise
/
Node.js

When every attempt passed to Promise.any() rejects, AggregateError.errors[0] belongs to the first input. If your backup endpoint fails before your primary endpoint, the primary failure still comes first in the array. Capture the source labels in the same order as the promises so you can tell which attempt produced each reason.

ECMAScript’s PerformPromiseAny algorithm assigns an index as it consumes each input and stores the rejection reason at that index. With an empty iterable it rejects with an AggregateError whose errors array is empty.

Reject the backup first and verify the labels#

Save this as promise-any-errors.mjs and run node promise-any-errors.mjs. The fixture uses manually controlled promises so the rejection order requires no timers or network. It was checked on Node.js 24.13.0 on macOS arm64.

import assert from 'node:assert/strict';
const primary = Promise.withResolvers();
const backup = Promise.withResolvers();
async function firstSuccessful(attempts) {
const labels = attempts.map(({ source }) => source);
if (labels.length === 0) return { status: 'no-attempts' };
const promises = attempts.map(({ promise }) => promise);
try {
return { status: 'ok', value: await Promise.any(promises) };
} catch (error) {
if (!(error instanceof AggregateError)) throw error;
return {
status: 'all-failed',
failures: error.errors.map((reason, index) => ({
source: labels[index],
reason,
})),
};
}
}
const attempts = [
{ source: 'primary', promise: primary.promise },
{ source: 'backup', promise: backup.promise },
];
const result = firstSuccessful(attempts);
attempts.reverse();
backup.reject('connection refused');
primary.reject('deadline exceeded');
const failure = await result;
assert.deepEqual(failure, {
status: 'all-failed',
failures: [
{ source: 'primary', reason: 'deadline exceeded' },
{ source: 'backup', reason: 'connection refused' },
],
});
console.log(failure.failures.map(({ source, reason }) => `${source}: ${reason}`).join('\n'));
assert.deepEqual(await firstSuccessful([]), { status: 'no-attempts' });
await assert.rejects(
Promise.any([]),
error => error instanceof AggregateError && error.errors.length === 0,
);
assert.deepEqual(await firstSuccessful([
{ source: 'primary', promise: Promise.reject(new Error('offline')) },
{ source: 'backup', promise: Promise.resolve('cached data') },
]), { status: 'ok', value: 'cached data' });

The output keeps input order even though the fixture calls the backup’s reject function first.

primary: deadline exceeded
backup: connection refused

The helper expects a dense array of labeled, already-created promises. It takes the label snapshot before awaiting, so a later reorder of the caller’s array cannot change attribution. It preserves each reason as a value because a rejection may carry a string, an Error, or another JavaScript value. The separate no-attempts result lets the caller report missing configuration without claiming an endpoint failed.

If you need the outcome of every attempt even after one succeeds, use Promise.allSettled() to wait for every outcome. The helper returns as soon as Promise.any() produces a successful value.

References:

Promise.any AggregateError: Match Errors to Their Inputs
https://laplusda.com/en/posts/promise-any-aggregateerror-input-order/
Author
Zero
Published at
2026-11-22
License
CC BY-NC-SA 4.0