Created
July 28, 2020 07:49
-
-
Save jenyayel/83dc6fc2e4d92f89df0ce28de4a47587 to your computer and use it in GitHub Desktop.
Wrapper for promise to resolve/reject externally and get indication whether the promise was fulfilled
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 Deferred<T = void> { | |
public resolve!: (value?: T | PromiseLike<T>) => void; | |
public reject!: (reason?: any) => void; | |
public readonly promise: Promise<T>; | |
private isFulfilled = false; | |
constructor() { | |
this.promise = new Promise( | |
(resolve, reject) => { | |
this.resolve = resolve; | |
this.reject = reject; | |
}) | |
.finally(() => { | |
this.isFulfilled = true; | |
}) as Promise<T>; | |
} | |
get fulfilled() { return this.isFulfilled; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment