Brandon Watson 506c170746
All checks were successful
continuous-integration/drone/push Build is passing
Renaming files
2021-12-28 18:14:23 -05:00

33 lines
681 B
TypeScript

/**
* Helper function to convert callbacks into promises
* @param func
*/
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));
};
}