Last active
June 23, 2021 16:07
-
-
Save vhqtvn/faf40fcba464124e311916099430bc53 to your computer and use it in GitHub Desktop.
Callback to 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 make_wrapper<T extends { [k: string]: any }>(root: T) { | |
type VHWithoutLastElement<T extends [...args: any]> = T extends [...args: infer P, last: any] ? P : never; | |
type VHLastElement<T extends [...args: any]> = T extends [...args: any, last: infer LastType] ? LastType : never; | |
type VHCallbackExtractValue<T extends any> = T extends ((err: any, value: infer ValueType) => void) ? (ValueType extends unknown ? void : ValueType) : never; | |
type VHPromisifier<T extends (...args: any) => any> = T extends (...args: infer P) => any ? ((...args: VHWithoutLastElement<P>) => Promise<VHCallbackExtractValue<VHLastElement<P>>>) : never; | |
return <fn_t extends keyof T>(fn: fn_t): VHPromisifier<T[fn_t]> => { | |
return promisify(root[fn]).bind(root); | |
}; | |
} | |
const pm2_ = make_wrapper(pm2); | |
await pm2_('connect')() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment