Skip to content

Instantly share code, notes, and snippets.

@drumnickydrum
Last active April 19, 2023 21:57
Show Gist options
  • Save drumnickydrum/70add30ec76d6ef1612e3873ac6e20f6 to your computer and use it in GitHub Desktop.
Save drumnickydrum/70add30ec76d6ef1612e3873ac6e20f6 to your computer and use it in GitHub Desktop.
[TS: Deferred Promise] Create a promise and pass along its res and rej #typescript #javascript #promise
// Andrew Burgess, YouTube
// https://www.youtube.com/watch?v=Yvhad4zdPqI
export class Deferred<T, E = unknown> {
promise: Promise<T>;
resolve: (value: T) => void = () => null;
reject: (reason?: E) => void = () => null;
constructor() {
this.promise = new Promise({resolve, reject} => {
this.resolve = resolve;
this.reject = reject;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment