Node.js: forEach inside async.series
Node.js: forEach inside async.series
我开始开发我的node.js应用程序,但我不能"异步思考"。在我的函数中,我必须执行一个查询,对于每一行,我必须执行另一个查询来计算最终结果并创建我的 json。我尝试这样做:
async.series([
function(callback) {
db.myCannedResponseList(req.params.websiteid, function(err,questions) {
if (err) return callback(err);
...
callback();
});
},
function(callback) {
async.forEachSeries(temp,function(quest,callback) {
db.getCannedAnswer(quest.id, function(err,answers) {
console.log(answers);
});
}, function(err) {
if (err) return next(err);
});
callback();
}
],
function(err) {
if (err) return next(err);
res.json(output);
});
但是有一个问题:getCannedAnswer的结果显示在res.json(output)之后。我该如何解决?
您需要在
forEachSeries
完成后调用回调。您还需要调用传递给forEachSeries
处理程序函数的回调:
async.forEachSeries(temp, function(quest, callback2) {
db.getCannedAnswer(quest.id, function(err, answers) {
console.log(answers);
// call the callback for forEachSeries so it can proceed:
callback2();
});
}, callback); // forEachSeries is done here, call the callback for async.series
你的异步函数是嵌套的,所以需要是你的回调。
这是一种方法:
async.series([
//async.apply(db.myCannedResponseList,req.params.websiteid), // This is equivalent to the first function below
//db.myCannedResponseList(req.params.websiteid,callback), // This is equivalent too
function(callback) {
db.myCannedResponseList(req.params.websiteid, callback);
},
function(callback) {
// I'm not sure forEach is the best choice here
// since it's callback isn't called with all the results so it won't be able to be forwarded...
// Personally, I use async.times for something like this.
async.times(temp.length,function(i,next) {
db.getCannedAnswer(temp[i].id,next);
},callback)
}
],function(err) {
if (err) return next(err);
res.json(output);
});
我不确定这个函数应该做什么,但是如果您在async.series
中遇到错误,您应该在成功时调用res.json(500,error)
或不调用res
。
一个好方法是完全分离此函数,并在完成后让它调用另一个回调。然后你会打电话给那里res
。
此外,您可以传递一个对象,以便对结果进行命名空间,而不是将数组传递给async.series
。
完整示例:
function getCannedStuff(data,callback){
async.series({
list : async.apply(db.myCannedResponseList,data.websiteid),
answer : function(cb) {
async.times(data.temp.length,function(i,next) {
db.getCannedAnswer(data.temp[i].id,next);
},cb)
}
},callback);
}
然后从另一个模块调用该函数:
getCannedStuff({
websiteid: req.params.websiteid,
temp:temp
},function(error,results) {
console.log(results.list);
console.log(results.answer);
if(err) return res.json(500,{error : error});
res.json(results)
});
您应该决定是否要在内部或外部控制错误getCannedStuff
但最好将它们传递下来,而不是一直if(err)
查看它们是否发生。
相关文章:
- node.js,async,restify:无法使restify调用在async.series内同步
- 如何防止bind在使用async.series时更改node.js中的原始对象
- 使用预定义函数的NodeJS async.series
- Node js async.series无法使用Express应用程序--响应发生得太早
- 等待函数结束(在 async.series 中)无法按预期工作
- 如何等待findOneAndUpdate完成,然后再继续async.series
- 为什么 async.series 只产生一个输出
- Node.js: forEach inside async.series
- 使用从 async.series 调用的原型函数中的“this”
- Async.而 async.而 在 async.series 中
- Async.series 不适用于 fs.readFile
- async.applyEachSeries无法引用'这'与async.series不同
- async.waterfall和async.series之间有什么区别
- 如何使用async.series来等待响应
- 结合异步.Each和async.series
- 用async.series的回调函数填充数组
- async.series和async.each未按预期工作
- rs.readFile in async.series 排序问题
- 异步 grunt 任务中的 async.series
- 如何在async.series中使用mocha测试