Created
July 24, 2018 06:16
-
-
Save amay077/39745bf49b456942a1819a11796e45e6 to your computer and use it in GitHub Desktop.
PromiseCompletionSource what is like TaskCompletionSource<T> in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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