Skip to solution
hardDSA

How do you implement a custom Promise.any polyfill?

541 views
01

Understand the problem

Question presented to candidate: "Promise.any takes an array of promises and resolves as soon as ANY one of them fulfills — it only rejects if every single one of them fails, and in that case it rejects with a special AggregateError containing all the individual failure reasons. Implement a polyfill for it. What happens if a rejection genuinely arrives before a fulfillment does? And what should the order of the errors be inside the AggregateError?"

What a strong answer should cover:

  • The outer promise resolves the instant ANY item fulfills — a rejection arriving first, even much faster than the eventual winner, must not affect the outcome at all.
  • The outer promise only rejects once EVERY item has rejected; that is the opposite failure condition from Promise.all.
  • The rejection reason is a real AggregateError, a built-in Error subclass added specifically for this purpose, whose .errors property is an array of every individual rejection reason.
  • That .errors array should preserve INPUT order (which promise was originally where), not the order the rejections actually happened to arrive in — this is the exact same indexing discipline Promise.all's polyfill needs for its results array.
  • An empty input array is a genuine edge case: it must reject immediately with an AggregateError containing zero errors, since there is nothing that could possibly fulfill.
  • AggregateError's constructor signature is new AggregateError(errorsIterable, message) — the errors come first, the message second, the reverse of what some engineers expect from plain Error.

Clarifying questions expected:

  • "Should the polyfill construct a real AggregateError, or is a plain Error with an attached .errors array acceptable if the environment predates AggregateError?" — a real interview-worthy compatibility question, since AggregateError is a newer addition than Promise itself.
  • "Do all the rejection reasons need to be captured even after the first one arrives, or can we stop tracking once we know the outer promise cannot possibly succeed?" — clarifies whether early-exit optimizations are in scope.

Code / implementation expected: Yes — a full, runnable polyfill plus a real test proving a fulfillment wins over a faster rejection, and that AggregateError.errors preserves input order.

promisespolyfillasync
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript async/Promise interview questions. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every result shown below is real, captured output from actually running the

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSThe polyfill plus a real test proving a fulfillment wins over a faster rejection, and error-order preservation (run directly)
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 145 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track