0
我有三个操作做彼此的NodeJS-异步:返回MySQL查询结果为循环
1.Fetch之后,从DB
2.Another MySQL查询一些行的for循环用于获取的一些数据和存储在变量
3.显示数据
对于我使用async
waterfall
方法。
async.waterfall([
function(callback){
//first sql and pass the result to second function
collection.getAllCollections(function (status,error,result) {
callback(null,result);
});
},
//running another query in loop with result with previous
function(result, callback){
for(var i=0;i<result.length;i++){
collection.getImages(result[i].id,function (status,error,user) {
//append the query result to old result
result[i]['users'] = user;
});
}
callback(null,result);
}
], function (err, result) {
console.log("result",result);
});
但问题最终result
不会因为第二个查询(在for循环是异步查询)
但是,在可以把'isDone'功能?我正在使用异步瀑布方法 – Jabaa
@Jabaa我更新了我的代码以阐明我的解决方案。这应该工作,但我没有测试它。 – jpschack
@Jabaa我再次更新了我的答案。使用async.parallel可以提供更好,更清洁,更高效的解决方案。这应该让你朝正确的方向发展。 – jpschack