node.js async.series functions not finishing in series -
i'm trying have functions execute in series using async.series. functions run not finish in series. i'm new node.js , i'm struggling figure out. here code i'm using.
async.series([ function(callback){ printrevision(peripheral, chars); callback(); }, function(callback){ setinterface(chars); callback(); }, function(callback){ performlooptest(peripheral, chars, 0); callback(); }, function(callback){ verifyresults(); callback(); }], function(err){ console.log(err) } ) function printrevision(peripheral, chars) { if (dis_fwrev_uuid in chars) { chars[dis_fwrev_uuid].read(function(error, data) { // ignore errors; missing fw revision ok if (data) { console.log( "bootloader firmware revision: " + data.tostring('ascii')); } }); } else { } } function setinterface(chars) { var buf = new buffer(4); var bytebuf = new buffer(1); var retbuf = 0; var uint8buf = 0; console.log("setting interface rs232") packet = chars[gim_control_char_uuid]; bytebuf.writeuint8(gim_set_interface_rs232, 0); packet.write(bytebuf, false, function(err) { checkerror(err); console.log("set interface done") }); } function performlooptest(peripheral, chars, writeindex) { var bytebuf = new buffer(1); control = chars[gim_tx_char_uuid]; packet = chars[gim_rx_char_uuid]; (var = 0; < write_buff_max_len; i++) { bytebuf.writeuint8(writebuf[i], 0); console.log("sending data"); packet = chars[gim_rx_char_uuid]; packet.write(bytebuf, false, function(err) { checkerror(err); }); } } function verifyresults() { console.log(writebuf); console.log(readbuf); } function savelatestdata(data) { console.log(data); readbuf.push(data); }
here output code above. seems jump on place. ideas on i'm doing wrong?
setting interface rs232 sending data sending data sending data sending data sending data sending data sending data sending data sending data sending data sending data sending data sending data sending data sending data sending data sending data sending data sending data sending data [ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57 ] [] undefined bootloader firmware revision: 1.0.7-release set interface done
your async series calling functions in turn call packet.write()
asynchronous. function in series array returns after kicking off write()
. write()
asynchronous finishes @ undetermined time in future.
if need them run in order, don't invoke callback()
(which says "ok, i'm done, run next function in series) in calling functions. instead, invoke callback()
in asynchronous functions callback.
for example, instead of in async series array:
function(callback){ printrevision(peripheral, chars); callback(); }
...do more this:
function(callback) { printrevision(peripheral, chars, callback); }
...then change printrevision()
:
function printrevision(peripheral, chars, callback) { if (dis_fwrev_uuid in chars) { chars[dis_fwrev_uuid].read(function(error, data) { // ignore errors; missing fw revision ok if (data) { console.log( "bootloader firmware revision: " + data.tostring('ascii')); } return callback(); }); } else { process.nexttick(callback) } }
note have not tested code gives idea.
you have make similar changes in other functions in async series array.
Comments
Post a Comment