Update to use homebridge types

This commit is contained in:
watsonb8
2020-12-11 21:59:29 -05:00
parent c28de00928
commit 38ffdec2db
15 changed files with 1101 additions and 1509 deletions

View File

@ -1,30 +1,32 @@
/**
* Helper function to convert callbacks into promises
* @param func
* @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;
}
export default function callbackify(
func: (...args: any[]) => Promise<any>
): Function {
return (...args: any[]) => {
const onlyArgs: any[] = [];
let maybeCallback: Function | null = null;
onlyArgs.push(arg);
}
for (const arg of args) {
if (typeof arg === "function") {
maybeCallback = arg;
break;
}
if (!maybeCallback) {
throw new Error("Missing callback parameter!");
}
const callback = maybeCallback;
func(...onlyArgs)
.then((data: any) => callback(null, data))
.catch((err: any) => callback(err))
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));
};
}