Last active
June 17, 2020 21:54
-
-
Save jacksteamdev/c7e2f886dd1e2b180f7b8bfcb6d5a2bc to your computer and use it in GitHub Desktop.
Deferred Promise
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
/** | |
* Use this in testing to control when a promise resolves | |
*/ | |
function defer<T>() { | |
let resolve: (value?: T) => void | |
let reject: (error: any) => void | |
const promise = new Promise<T>((res, rej) => { | |
resolve = res | |
reject = rej | |
}) | |
return Object.assign(promise, { | |
resolve(value?: T) { | |
resolve!(value) | |
}, | |
reject(error: any) { | |
reject!(error) | |
}, | |
}) as Promise<T> & { | |
resolve: typeof resolve | |
reject: typeof reject | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment