I am in the process of writing an internal NPM module for my company so that applications can interact with a hardware device we have, which we already have a library written for communicating with it. The problem is, one method I am trying to write needs to execute asynchronously. I want the function to send a command to read from the device, wait for it to return (the device library handles this), and then I parse the results. What I am hoping for is for other developers to be able to call exports.getResolution() to get the values. Here is a snippet of my file with the relevant parts:
var async = require('async');
exports.getResolution = async.series([
function(callback) {
board.sendAndReceive(bufferlib.generateBuffer("read", "0x00001220", "0x00000004"), (function(received) {
var hex = bufferlib.sortReceivedHex(received);
var status = parseInt('0x' + hex.substring(0, 1), 16);
var verticalResolution = parseInt('0x' + hex.substring(1, 4), 16);
var horizontalResolution = parseInt('0x' + hex.substring(5, 9), 16);
callback(null, {
'status': status,
'vertical': verticalResolution,
'horizontal': horizontalResolution
});
}));
}
],
// optional callback
function(err, results) {
status = results[0];
return results[0];
});
console.log(exports.getResolution);
I have tried callback hell, Promises, bluebird, ES6 async functions, and a bunch of other solutions but I just can't figure this one out. My most recent attempt uses the async node module to try and execute the code asynchronously, which works, but now I just need to get exports.getResolution to return the actual value the final callback receives. What am I doing wrong? What can I do to make this work? Thank you.