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
AggregateErrorwhose message is"All promises were rejected"and whoseerrorsarray holds the rejection reasons in input order, - for an empty input, rejects with such an
AggregateErrorwhoseerrorsis[].
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
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 8. ⌘Enter runs, ⌘⇧Enter submits.