polyfillpolyfill/polyfill-service

Add Promise.withResolvers Polyfill

pckilgore opened this issue · 2 comments

What

Missing Promise.withResolvers

Details

react-pdf recently upgraded and no longer includes a polyfill for this API, which is now baseline available in 2024.

We've got some older safari clients breaking.

Happy to make a PR:

ref: CoreJS Polyfill

Code roughly:

if (typeof Promise.withResolvers === 'undefined') {
  window.Promise.withResolvers = function () {
    let resolve, reject;
    const promise = new Promise((res, rej) => {
      resolve = res;
      reject = rej;
    });
    return { promise, resolve, reject };
  }
}

Your suggested polyfill isn't compliant as it doesn't handle subclassing:

Promise.withResolvers() is generic and supports subclassing, which means it can be called on subclasses of Promise, and the result will contain a promise of the subclass type.

From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers