Extract reenumerate() as an async function in reenumerate.js

This commit is contained in:
László Monda
2018-01-30 18:36:16 +01:00
parent 5099e904fc
commit 88c42d58b1

View File

@@ -23,8 +23,10 @@ if (enumerationModeId === undefined) {
process.exit(1);
}
console.log(`Trying to reenumerate as ${enumerationMode}...`);
setInterval(() => {
function reenumerate(enumerationModeId, bootloaderTimeoutMs) {
return new Promise((resolve, reject) => {
console.log(`Trying to reenumerate as ${enumerationMode}...`);
const intervalId = setInterval(() => {
pollingTimeoutMs -= pollingIntervalMs;
const foundDevice = HID.devices().find(device =>
@@ -32,12 +34,16 @@ setInterval(() => {
if (foundDevice) {
console.log(`${enumerationMode} is up`);
process.exit(0);
resolve();
clearInterval(intervalId);
return;
}
if (pollingTimeoutMs <= 0) {
console.log(`Couldn't reenumerate as ${enumerationMode}`);
process.exit(1);
reject();
clearInterval(intervalId);
return;
}
let device = uhk.getUhkDevice();
@@ -48,4 +54,10 @@ setInterval(() => {
jumped = true;
}
}, pollingIntervalMs);
}, pollingIntervalMs);
})
};
(async function() {
await reenumerate(enumerationModeId, bootloaderTimeoutMs);
})();