Polyfills… Polyfills… Polyfills (Part 2)

Sourav Saha
2 min readMay 8, 2022

--

This is a continuation of my previous post (Check Here).

In this post, I’ll be explaining “Polyfills related to Promise asked in interviews”.

Promise.all (Most Asked)

Polyfill of Promise.all is the most commonly asked polyfills, with knowledge of this one, you can understand the rest of the polyfills that will be covered in this article.

  1. To understand this, first you need to know how Promise.all works.
  2. Promise.all takes an iterable and returns a Promise. If any of the values in iterable rejects then the whole promise rejects. It’s resolved if all the promises are resolved.

Now let’s build our own Promise.all

Promise.all Polyfill.

Promise.allSettled

  1. This is similar to Promise.all, the difference being insted of array of resolved promises values it return an array of object, with the state in status key (fulfilled/rejected) and the value in value key.
  2. It is not immediately rejected after it encounters a catch, insted the error is push to result array with the reason and the status being set to rejected.
Promise.allSettled Polyfill

Similary, you can write Polyfill for Promise.any and Promise.race.

As a bonus I’ll be writing a Polyfill of Promise itself.

Custom Promise

That’s all folks……

Follow me on LinkedIn.

--

--