Implement Promise.any

MediumPromises & Async20 min4 tests

Implement your own version of Promise.any that resolves with the first fulfilled promise. If all promises reject, reject with an AggregateError.

Requirements

  • Resolve as soon as any promise fulfills
  • If ALL promises reject, reject with an array of all rejection reasons
  • Handle non-promise values (treat as immediately resolved)

Example

promiseAny([Promise.reject('a'), Promise.resolve('b'), Promise.reject('c')]); // 'b'
Hints (4)
  1. Return a new Promise.
  2. The first promise to resolve should resolve the outer promise.
  3. Track all rejections in an array.
  4. Only reject the outer promise when ALL inner promises have rejected.

Topics

  • Promises & Async

Asked at

Meta · Netflix · Apple

Join Us
blur