Skip to content
Onsite

Promise.any Polyfill

MediumP2Coding

Implement promiseAny(promises), a polyfill for Promise.any.

It takes an iterable of values or promises and returns a promise that:

  • resolves with the value of the first input to fulfil (plain values count as fulfilled); rejections before that are ignored,
  • if every input rejects, rejects with an AggregateError whose message is "All promises were rejected" and whose errors array holds the rejection reasons in input order,
  • for an empty input, rejects with such an AggregateError whose errors is [].

Do not call Promise.any.

Example 1

Input
promiseAny([Promise.reject('a'), delay(10, 'b')])
Output
resolves with 'b'

Example 2

Input
promiseAny([Promise.reject('a'), Promise.reject('b')])
Output
rejects with AggregateError, errors = ['a', 'b']

Constraints

  • 0 <= number of items <= 10^4

Hints

Run checks the 3 visible tests, Submit all 8.