Skip to content

Commit

Permalink
replacing async try/catch blocks with IIFEs
Browse files Browse the repository at this point in the history
  • Loading branch information
gniezen committed Dec 6, 2019
1 parent 58cc0de commit 837b352
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 31 deletions.
53 changes: 30 additions & 23 deletions lib/drivers/bluetoothLE/bluetoothLEDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,26 @@ module.exports = (config) => {
cb(null, { deviceInfo });
},

async connect(progress, data, cb) {
try {
debug('in connect!');
connect(progress, data, cb) {
debug('in connect!');

(async () => {
await cfg.deviceComms.ble.connectTimeout();
} catch (error) {
})().then(() => {
return cb(null, data);
}).catch((error) => {
debug('Error in connect: ', error);
return cb(error, null);
}
return cb(null, data);
});
},

async getConfigInfo(progress, data, cb) {
getConfigInfo(progress, data, cb) {
debug('in getConfigInfo', data);
progress(0);

try {
(async () => {
_.assign(cfg.deviceInfo , await cfg.deviceComms.ble.getDeviceInfo());

})().then(() => {
if (!cfg.deviceInfo.name.startsWith('CareSens')) {
return cb (new Error('We don\'t currently support this meter.'));
}
Expand All @@ -74,14 +77,14 @@ module.exports = (config) => {
cfg.deviceInfo.deviceId = `${[cfg.deviceInfo.manufacturers]}-${cfg.deviceInfo.model}-${remote.getGlobal('bluetoothDeviceId')}`;
data.deviceModel = cfg.deviceInfo.model; // for metrics
cfg.builder.setDefaults({ deviceId: cfg.deviceInfo.deviceId });
} catch (error) {
return cb(null, data);
}).catch((error) => {
debug('Error in getConfigInfo: ', error);
return cb(error, null);
}

return cb(null, data);
});
},

async fetchData(progress, data, cb) {
fetchData(progress, data, cb) {
debug('in fetchData', data);

cfg.deviceComms.ble.once('data', (result) => {
Expand All @@ -90,11 +93,11 @@ module.exports = (config) => {
return cb(null, data);
});

try {
(async () => {
await cfg.deviceComms.ble.getAllRecords();
} catch (error) {
return cb (error, null);
}
})().catch((error) => {
return cb(error, null);
});
},

processData(progress, data, cb) {
Expand Down Expand Up @@ -188,12 +191,16 @@ module.exports = (config) => {
cb(null, data);
},

async cleanup(progress, data, cb) {
cleanup(progress, data, cb) {
debug('in cleanup');
await cfg.deviceComms.ble.disconnect();
progress(100);
data.cleanup = true;
cb();

(async () => {
await cfg.deviceComms.ble.disconnect();
})().then(() => {
progress(100);
data.cleanup = true;
return cb();
});
},
};
};
19 changes: 11 additions & 8 deletions lib/drivers/trividia/trueMetrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,17 +286,20 @@ module.exports = (config) => {
});
},

async fetchData(progress, data, cb) {
fetchData(progress, data, cb) {
debug('in fetchData', data);
try {
const results = await driver.commandResponse(COMMANDS.GET_RESULTS);
let results = null;

(async () => {
results = await driver.commandResponse(COMMANDS.GET_RESULTS);
})().then(() => {
data.glucose = TrueMetrix.filterByType(results, TYPES.GLUCOSE);
[data.checksum] = TrueMetrix.filterByType(results, TYPES.CHECKSUM);
cb(null, data);
} catch (error) {
debug('Error in fetchData: ', error);
cb(error, null);
}
return cb(null, data);
}).catch((error) => {
debug('Error in connect: ', error);
return cb(error, null);
});
},

processData(progress, data, cb) {
Expand Down

0 comments on commit 837b352

Please sign in to comment.