我使用异步库以串行方式执行数组,如下所示
async.eachSeries(Object.keys(req.body),function(elem, callback){
});
在此同步中,我调用的后端也是异步,如下所示:
async.eachSeries(Object.keys(req.body),function(elem, callback){
client.search(Query).then(function(Data){
//Some computation with result(data)
});
});
最后我结束循环以进行如下计算:
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);
});
但实际上我得到的是,在获得后端的结果之前,后端调用async会执行回调。如何限制或等待(不睡觉)后端调用完成并执行下一个代码。请分享您的想法。提前谢谢。