I was referring this nodetuts tutorial on coordinating parallel calls in NodeJs. http://nodetuts.com/series/asynchronous-programming/mastering-asynchronous-programming-02.html
There is something that I needed your help in understanding.
callback1,callback12, callback3 are the functions that need to be executed parallelly. Each takes their own time to execute.
But their results are stored in the results array in the respective order
[resultofcallback1, resultofcallback2, resultofcallback3]
The only contract of the final callback to execute only once and show the results in a proper order, as mentioned.
For that we have a Boolean calledBack
to make sure it is executed only once.
Please see the code below
My argument is while executing callBack1.
Inside handleResult
: pending =0
, order = 0, pending is incremented to 1
Let's say this callback takes the least time to execute.
Inside the return function for this handleResult pending is decremented
which makes pending 0
. Let's say by this time no other callBacks(callback1 ,callback2)
has reached their handlResult function, thus pending remains 0. So this if(!pending) assert and the final callBack is called callback(null, results);
outputting only one result something like [resultofCallback1,'','']
, the rest two empty, as the final callback should be called only once.
module.exports = function composedCall(args, cb){
var pending = 0;
var results = [];
var calledBack = false;
callback1(args,handleResult());
callback2(args,handleResult());
callback3(args,handleResult());
function handleResult(){
var order = pending;
pending ++;
return function(err, result){
pending --;
if(err){
callback(err);
}else{
results[order] = result;
if(!pending){
callback(null, results);
}
}
}
}
function callback(err, value){
if(! calledBack){
calledBack = true;
cb(err, value)
}
}
}
function callback1(args ,callback){
setTimeout(callback, randomTimeout(), null, 1);
}
function callback2(args ,callback){
setTimeout(callback, randomTimeout(), null, 2);
}
function callback3(args ,callback){
setTimeout(callback, randomTimeout(), null, 3);
}
//utils
function randomTimeout(){
return Math.floor(Math.random() * 1e3);
}
function randomValue(){
return Math.floor(Math.random() * 1e10);
}
Is this a right approach to make coordinating parallel call Did I miss something?