Skip to content
Onsite

Promise.all Polyfill

MediumP1Coding

Implement promiseAll(promises), a polyfill for Promise.all.

It takes an array (or any iterable) of values or promises and returns a promise that:

  • resolves with an array of the results in input order once all of them have resolved (plain non-promise values count as already resolved),
  • rejects with the reason of the first promise that rejects,
  • resolves with [] for an empty input.

Do not call Promise.all.

Example 1

Input
promiseAll([Promise.resolve(1), 2, delay(10, 3)])
Output
resolves with [1, 2, 3]

Example 2

Input
promiseAll([Promise.resolve(1), Promise.reject('no')])
Output
rejects with 'no'

Constraints

  • 0 <= number of items <= 10^4
  • Result order must follow input order, not completion order.

Hints

Run checks the 3 visible tests, Submit all 8.