I am using async library for executing an array in serial manner as follows
async.eachSeries(Object.keys(req.body),function(elem, callback){
});
Inside this sync i am calling the backend which is also async as follows:
async.eachSeries(Object.keys(req.body),function(elem, callback){
client.search(Query).then(function(Data){
//Some computation with result(data)
});
});
And finally i am ending the loop to do some computation as follows:
var bodyLength = Object.keys(req.body).length;
async.eachSeries(Object.keys(req.body),function(elem, callback){
client.search(Query).then(function(Data){
//Some computation with result(data)
});
if(--bodyLength === 0){
callback(null, notMyLocData, myLocData);------->This line is executed before computation from backend
}
}, function(err, notMyLocData, myLocData){
console.log("notMyLocData");
console.log(notMyLocData);
console.log("myLocData");
console.log(myLocData);
});
But actually what i am getting is, as backend call in async before getting result for backend the callback is executed. How can i restrict or wait(not sleep) for the backend call to finish and executed the next code.Please share your ideas. Thanks in advance.