Skip to content

Instantly share code, notes, and snippets.

@kbkk
Created April 15, 2020 13:02
Show Gist options
  • Save kbkk/c96242639a418df270dc02444301a0f2 to your computer and use it in GitHub Desktop.
Save kbkk/c96242639a418df270dc02444301a0f2 to your computer and use it in GitHub Desktop.
TypeScript Deferred Promise
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