Javascript — Polyfill for Promise.all, allSettled, race and any

Eishta Mittal
2 min readOct 30, 2022

There are 6 static methods in the Promise class.

Promise.all

  • Wait for all promises to be fulfilled, or for any to be rejected.
  • If the returned promise fulfills, it is fulfilled with an aggregating array of the values from the fulfilled promises, in the same order as defined in the iterable of multiple promises.
  • If it rejects, it is rejected with the reason from the first promise in the iterable that was rejected.

Polyfill:-

Promise.allSettled

  • Wait until all promises have settled (each may fulfill or reject).
  • Returns a Promise that fulfills after all of the given promises is either fulfilled or rejected, with an array of objects that each describe the outcome of each promise.

Polyfill:-

Promise.any

Takes an iterable of Promise objects and, as soon as one of the promises in the iterable fulfills, returns a single promise that fulfills with the value from that promise.

Polyfill:-

Promise.race

  • Wait until any of the promises is fulfilled or rejected.
  • If the returned promise fulfills, it is fulfilled with the value of the first promise in the iterable that fulfilled.
  • If it rejects, it is rejected with the reason from the first promise that was rejected.

Polyfill:-

Sources

MDN

--

--