Last active
January 15, 2018 09:14
-
-
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?)
This file contains hidden or 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 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