Created
April 15, 2020 13:02
-
-
Save kbkk/c96242639a418df270dc02444301a0f2 to your computer and use it in GitHub Desktop.
TypeScript Deferred Promise
This file contains 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
function deferPromise<T>(): { | |
resolve: ( value?: ( PromiseLike<T> | T ) ) => void; | |
reject: ( reason?: unknown ) => void; | |
promise: Promise<T>; | |
} { | |
let resolve: ( value?: T | PromiseLike<T> ) => void; | |
let reject: ( reason?: unknown ) => void; | |
// eslint-disable-next-line promise/param-names | |
const promise: Promise<T> = new Promise( ( promiseResolve, promiseReject ) => { | |
resolve = promiseResolve; | |
reject = promiseReject; | |
} ); | |
return { resolve: resolve!, reject: reject!, promise }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment