Skip to content

Instantly share code, notes, and snippets.

@NickBolles
Created November 23, 2020 14:20
Show Gist options
  • Save NickBolles/bce9e13faa8c6c2b6ce61b4430a392d8 to your computer and use it in GitHub Desktop.
Save NickBolles/bce9e13faa8c6c2b6ce61b4430a392d8 to your computer and use it in GitHub Desktop.
export class ResolvablePromise<T = any> implements Promise<T> {
[Symbol.toStringTag]: string;
__promise: Promise<T>;
resolve!: (value: T) => void;
reject!: (reason: T) => void;
constructor() {
this.__promise = new Promise<T>((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}
then<TResult1 = T, TResult2 = never>(
onfulfilled?: (value: T) => TResult1 | PromiseLike<TResult1>,
onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>
): Promise<TResult1 | TResult2> {
return this.__promise.then(onfulfilled, onrejected);
}
catch(...args: any[]) {
return this.__promise.catch(...args);
}
finally(...args: any[]) {
return this.__promise.finally(...args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment