Most (if not all) callbacks are registered

This commit is contained in:
watsonb8
2019-06-15 12:04:20 -04:00
parent 0ff8ce8622
commit ea5b7ba054
4 changed files with 111 additions and 7 deletions

27
src/Util/Callbackify.ts Normal file
View File

@ -0,0 +1,27 @@
export default function callbackify(func: (...args: any[]) => Promise<any>): Function {
return (...args: any[]) => {
const onlyArgs: any[] = [];
let maybeCallback: Function | null = null;
for (const arg of args) {
if (typeof arg === 'function') {
maybeCallback = arg;
break;
}
onlyArgs.push(arg);
}
if (!maybeCallback) {
throw new Error("Missing callback parameter!");
}
const callback = maybeCallback;
func(...onlyArgs)
.then((data: any) => callback(null, data))
.catch((err: any) => callback(err))
}
}