← JavaScript Coding

Promise.any Polyfill

mediumpromisespolyfillasync
Asked at
Adobe
Amazon
Google
Microsoft
PayPal
Adobe, Amazon, Google, Microsoft, PayPal

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 AggregateError containing all rejection reasons when all inputs reject.
  • Reject immediately with AggregateError for 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.

Premium

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
Unlock the full solution →

Already a member? Log in to open the workspace.

More JS Coding questions

View all JS Coding →
JS CodingEasy

City Fetcher API

JS CodingHard

promise.all( )

JS CodingHard

promise.allSettled( )

JS CodingHard

promise.any( )

JS CodingHard

promise.race( )

JS CodingEasy

promise.reject()

Explore related prep hubs

  • All JavaScript coding
  • JavaScript interview questions
  • JS polyfill interview questions
  • Debounce & throttle questions
  • React interview hub
  • Adobe interview
  • Amazon interview
  • Google interview
  • Microsoft interview
  • PayPal interview