Skip to content

Instantly share code, notes, and snippets.

@SystemDisc
Last active January 15, 2018 09:14
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 SystemDisc/80854e40b2ef9b50a0158757daed9bf6 to your computer and use it in GitHub Desktop.
Save SystemDisc/80854e40b2ef9b50a0158757daed9bf6 to your computer and use it in GitHub Desktop.
class definition for DeferredPromise (why can't they just add it to the ES6 spec?)
export class DeferredPromise<T> {
public promise: Promise<T>;
private resolved: PromiseLike<T>;
public constructor() {
this.promise = new Promise((resolve, reject) => {
if (this.resolved) {
resolve(this.resolved);
}
this.resolve = resolve;
this.reject = reject;
});
}
public resolve(value?: T | PromiseLike<T>) {
if (!this.resolved) {
this.resolved = Promise.resolve(value);
}
}
public reject(reason?: any) {
if (!this.resolved) {
this.resolved = Promise.reject(reason);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment