Skip to content

Instantly share code, notes, and snippets.

@johannschopplich
Created June 24, 2024 08:22
Show Gist options
  • Save johannschopplich/5be256c8d297f953fa2c3975e4d9fb5b to your computer and use it in GitHub Desktop.
Save johannschopplich/5be256c8d297f953fa2c3975e4d9fb5b to your computer and use it in GitHub Desktop.
Polyfill for `Promise.witResolvers()` in TypeScript
Promise.withResolvers ??= function <T>() {
let resolve: PromiseWithResolvers<T>["resolve"];
let reject: PromiseWithResolvers<T>["reject"];
const promise = new Promise<T>((res, rej) => {
resolve = res;
reject = rej;
});
return { promise, resolve: resolve!, reject: reject! };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment