Skip to content

Instantly share code, notes, and snippets.

@jenyayel
Created July 28, 2020 07:49
Show Gist options
  • Save jenyayel/83dc6fc2e4d92f89df0ce28de4a47587 to your computer and use it in GitHub Desktop.
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
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