Node.js Parallel calls to same child rest service and aggregating response -
i want call parent rest service child rest service. number of times child service called depends on parameters parent rest services. once call child service instance concurrently different parameters. want combine responses instances of child service. using below snippet. don't want use timeout. should either timeout or when calls of child service on ever lesser.
for( i=0; i<length; i++) { url=accountid[i] +'+'+sortcode[i] +'+' +accountholdername[i]; micro(url ,filter[i],function(resp) { this.resutlobject[count]=resp; console.log("count"+count); count=count+1; }.bind( {resutlobject: resutlobject} )); }//end of settimeout(function () { console.log("in time out"); res.end(json.stringify(resutlobject || {}, null, 2)); },500);
you can use async module async. provides parallel foreach loop.
var obj = {dev: "/dev.json", test: "/test.json", prod: "/prod.json"}; var configs = {}; async.foreachof(obj, function (value, key, callback) { fs.readfile(__dirname + value, "utf8", function (err, data) { if (err) return callback(err); try { configs[key] = json.parse(data); } catch (e) { return callback(e); } callback(); }) }, function (err) { if (err) console.error(err.message); // configs map of json data dosomethingwith(configs); })
here in example reading files listed in parameters. can task
Comments
Post a Comment