AniUI Academy
medium+250 XPPractice

Implement Promise.race from scratch

Racing promises is how you build timeouts, how you pick whichever of two endpoints answers first, and how you abandon work when the user navigates away. Writing it yourself makes the settle-once rule obvious.

Implement race(promises) without using Promise.race. It returns a promise that settles exactly like the first input to settle: fulfilling with its value, or rejecting with its reason. Values in the array that are not promises count as already fulfilled. Once the returned promise has settled, later settlements of the other inputs must be ignored. An empty array returns a promise that never settles.

What it has to do

  • Fulfil with the value of the first input to fulfil.
  • Reject with the reason of the first input to reject.
  • Treat non-promise entries as already fulfilled values.
  • Ignore every settlement after the first.
  • Return a promise that never settles for an empty array.

Your workspace

Try it yourself
Loading playground...

Ready to check it?

5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.

AI Crack & Solution Assist

Stuck? Get instant AI hints or break down the optimal solution.

Stuck? The javascript course covers everything this challenge needs.