Promise.any Polyfill
Problem statement
Implement promiseAny(iterable), a polyfill for Promise.any.
It should resolve as soon as any input promise fulfills. It should reject only when every input rejects.
Requirements
- Accept an iterable of values or promises.
- Resolve with the first fulfilled value.
- Reject with an
AggregateErrorcontaining all rejection reasons when all inputs reject. - Reject immediately with
AggregateErrorfor an empty iterable. - Preserve rejection reasons in input order.
Requirements & constraints
- Do not call native `Promise.any`.
- Handle empty input.
- Preserve error ordering by input position.
How to approach Promise.any Polyfill
The strategy an interviewer expects you to reach for.
Same contract as Promise.any, but you may be polyfilling an older runtime without AggregateError. Naive move Reject with a plain Error and drop the reason list. Callers that expect error.errors break. Where it breaks You need a fallback AggregateError shape when the global is missing, while still resolving on first fulfill and waiting for all rejects otherwise. The fix Keep the any state machine. When fabricating AggregateError, prefer the native constructor; otherwise create an Error and attach an errors array so debugging still works. Remember: Polyfill the error shape, not just the happy path. Say this in the interview: I'd implement first-success resolution, collect all rejections, and polyfill AggregateError so error.errors still carries every reason.
The full solution is part of HelloFrontend Pro
The question above is free to read in full. Upgrade to unlock the interactive workspace and the senior-level walkthrough that go with it.
- Runnable editor with the hidden test suite
- Progressive hints that unlock as you get stuck
- Senior-level reference solution with a line-by-line walkthrough
Already a member? Log in to open the workspace.