call(back)
FrontendmediumSign in to star problems

Implement Promise.any

Implement promiseAny(promises), which behaves like Promise.any for an array, without calling it. Promise.all, allSettled, race and any are switched off while it runs.

  • Resolve with the value of the first input to fulfil, however many others have already rejected.
  • Reject only once every input has rejected, with an AggregateError whose errors array holds the reasons in input order (its message may be anything).
  • Accept plain values beside promises.
  • An empty input rejects at once, with an AggregateError whose errors is [].

Everything runs on a virtual clock, so the grader sees exactly when the result settles.

Hints

Hint 1

Promise.all inverted: pass resolve straight through as the fulfilment handler, and do the counting in the rejection handler.

Hint 2

Store each reason at its input's index, not in arrival order, so the errors array reads like the input did.

Similar Problems6