Promise.allSettled / race / any Polyfills

PromisesPolyfill

Implement all three Promise utility methods.

Promise.myAllSettled(promises)

  • Never rejects
  • Resolves with { status: "fulfilled", value } or { status: "rejected", reason } for each input

Promise.myRace(promises)

  • Settles as soon as the first promise settles (resolve or reject)

Promise.myAny(promises)

  • Resolves with the first fulfilled value
  • Only rejects if all promises reject, via AggregateError

Examples

Example 1
Input
Promise.myAllSettled([Promise.resolve(1), Promise.reject('err')])
  .then(console.log)
Output
[{ status: 'fulfilled', value: 1 }, { status: 'rejected', reason: 'err' }]
Explanation
allSettled always resolves, reporting the outcome of every promise.
Example 2
Input
Promise.myRace([
  new Promise(r => setTimeout(() => r('slow'), 200)),
  new Promise(r => setTimeout(() => r('fast'), 50)),
]).then(console.log)
Output
fast
Explanation
The promise that settles first wins.

Notes

  • Mental map: all (every success), allSettled (every outcome), race (first either way), any (first success)

Hints

Read the full write-up for Promise.allSettled / race / any Polyfills
</>JavaScript
Loading editor…
Test Result
Run your code, or Submit to test it