polyfills: add Promise.withResolver

This commit is contained in:
thislight 2024-12-04 22:57:41 +08:00
parent bffa896184
commit 0fca81dc93
No known key found for this signature in database
GPG key ID: FCFE5192241CCD4E
2 changed files with 47 additions and 0 deletions

26
types/lib.esnext.promise.d.ts vendored Normal file
View file

@ -0,0 +1,26 @@
interface AnyPromiseWithResolvers<T, Instance> {
promise: Instance;
resolve: (value: T | PromiseLike<T>) => void;
reject: (reason?: any) => void;
}
type AnyPromiseConstructor<T> = new (
executor: (
resolve: PromiseWithResolvers<T>["resolve"],
reject: PromiseWithResolvers<T>["reject"],
) => void,
) => Promise<T>;
interface PromiseConstructor {
/**
* Creates a new Promise and returns it in an object, along with its resolve and reject functions.
* @returns An object with the properties `promise`, `resolve`, and `reject`.
*
* ```ts
* const { promise, resolve, reject } = Promise.withResolvers<T>();
* ```
*/
withResolvers<T, K extends AnyPromiseConstructor<T>>(
this: K,
): AnyPromiseWithResolvers<T, InstanceType<K>>;
}