AniUI Academy

Implement Promise.any from scratch

mediumAsync & Promises+250 XPPractice

Three CDNs host the same asset. You want whichever answers successfully first, and you only want to show an error if all three are down. Promise.race cannot do this: it settles on the first rejection.

Implement any(promises) without using Promise.any. It resolves with the value of the first input to fulfil, ignoring any rejections before that. If every input rejects, reject with an Error whose message is exactly "All promises were rejected" and whose errors property is an array of the rejection reasons in input order. An empty array rejects the same way, with errors set to an empty array. Non-promise entries count as already fulfilled.

What it has to do

  • Resolve with the first fulfilled value, whatever order the inputs settle in.
  • Ignore rejections while any input can still fulfil.
  • Reject with an Error whose message is All promises were rejected when all inputs reject.
  • Put the rejection reasons on the error's errors property, in input order.
  • Reject with an empty errors array for an empty input array.

Where it's been asked

  • Google
  • Microsoft

From public interview reports — a problem very like this one has shown up in a round at these companies. Not an exhaustive list.

Stuck? The javascript course covers everything this challenge needs.

Try it yourself
Loading playground...

Ready to check it?

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

Need a nudge?

Reveal a conceptual hint or the reference solution. Both were written and reviewed with the problem — no runtime magic.

Discussion

Sign in to post
Sign in to join the conversation.Sign in

No comments yet. Be the first to say something.