Skip to content

Instantly share code, notes, and snippets.

@amay077
Created July 24, 2018 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amay077/39745bf49b456942a1819a11796e45e6 to your computer and use it in GitHub Desktop.
Save amay077/39745bf49b456942a1819a11796e45e6 to your computer and use it in GitHub Desktop.
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