← JavaScript Coding

promise.race( )

hard

Problem statement

Implement customPromiseRace(promises), a version of Promise.race.

The returned promise should settle as soon as the first input settles, whether it resolves or rejects.

How to approach promise.race( )

The strategy an interviewer expects you to reach for.

Promise.race is a timeout primitive: the first settlement wins — success or failure. Naive move Only listen for fulfills, or wait for all and pick min latency. Both miss the point. Where it breaks A fast rejection must reject the race. Non-promises must compete too (they settle as already-resolved). Empty arrays hang forever in the real API — call that out. The fix For each input, Promise.resolve(input).then(resolve, reject) onto one outer promise. The first call to resolve/reject wins; later settlements no-op. Remember: Race = first settlement, not first success. Say this in the interview: I'd wire every input into one outer promise so the first fulfill or reject settles it, and I'd mention empty-array hang as a known footgun.

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 CodingEasy

promise.reject()

JS CodingMedium

composeAsync

Explore related prep hubs

  • All JavaScript coding
  • JavaScript interview questions
  • JS polyfill interview questions
  • Debounce & throttle questions
  • React interview hub