javascript - How to execute two asynchronous function in sequence -
how make asynchronous javascript function execute in sequence
socket_connection.on('request', function (obj) { process(obj); }); function process(obj){ readfile(function(content){ //async //read content //modify content writefile(content, function(){ //async //write content }); }); }
this results in sequence:
read content read content modify content modify content write content write content
how can enforce:
read content modify content write content read content modify content write content
what want known blocking. in case want block second or consecutive request until first request completes.
my honest opinion - nodejs not suitable platform if want block call. nodejs not able perform freely should be.
that being said can similar -
var isexecuting = false; socket_connection.on('request', function (obj) { // in other languages have done similar // while(isexecuting); // may not work here. var intrvl = setinterval(function(){ if(!isexecuting){ clearinterval(intrvl); isexecuting=true; process(obj); } },0); // if necessary put number greater 0 }); function process(obj){ readfile(function(content){ //async //read content //modify content writefile(content, function(){ //async //write content isexecuting = false; }); }); }
Comments
Post a Comment