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 type Work = (...args: any[]) => any; | |
// Pass a function that provides a callback as single argument and returns another function F that calls that | |
// callback at some point. The result of makeContinuable will be a function that returns a promise that will | |
// resolve to the return value of F, once the callback is called. | |
export function makeContinuable<F extends Work>(wrapperFn: (cb: () => void) => F) { | |
return async (...args: Parameters<F>) => { | |
const outerPromise = new Promise<ReturnType<F>>(async (resolve1) => { | |
let res = null as ReturnType<F>; | |
// create another promise in whose executioner we'll call the passed function |
NewerOlder