I have a function that, when you press a key, sends a keycode over a serial port. However, in this circumstance, onKey
's callback is getting activated faster than the data can be written to the serial port, so I need to use a drain function which sends all data and calls back when it's done.
How do I wait for the drainSerialPort
callback to execute before attempting to send the next key code? I assume there's a better way to structure the three functions.
onKey(function(key) {
writeSerial(key, function() {
drainSerialPort(function() {
// Wait for this callback to be called to continue
});
});
});
According to your requirement the best option for you is asynchronus. First your function onKey is executed, after when it completed function writeSerial is executed.
var onKey = function (key) {
};
async.eachSeries(onKey, writeSerial, function (err) {
if (err) {
res.json({status: 0, msg: "OOPS! How is this possible?"});
}
drainSerialPort(function () {
// Wait for this callback to be called to continue
});
res.json("Series Processing Done");
})
var writeSerial = function (key) {
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.