Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PromiseCompletionSource what is like TaskCompletionSource<T> in C#
export class PromiseCompletionSource<T> {
public readonly promise: Promise<T>;
private resolver: (x?: T) => void;
private rejector: (reason?: any) => void;
constructor() {
this.promise = new Promise<T>((resolve, reject) => {
this.resolver = resolve;
this.rejector = reject;
});
}
public resolve(x: T) {
if (this.resolver) {
this.resolver(x);
}
}
public reject(reason?: any) {
if (this.rejector) {
this.rejector(reason);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment