【发布时间】:2015-09-12 20:03:37
【问题描述】:
几个小时以来,我一直在研究这段代码。此外,我之前多次使用过“async.each”。可能是代码只需要一组不同的眼睛来工作。这是代码和问题。
async.each(process.argv, function(file, callback){
runFile(file, callback);
}, function(err){
console.log("Here is the error!:" + err +"files to unstage: " + filesToUnstage)
if(err)
console.log(err);
else{
if(filesToUnstage) {
__unstage();
}
}
});
运行文件:
function runFile(filename, callback) {
// Do a bunch of stuff ....
__parseOutput(output, callback);
}
__parseOutput:
function __parseOutput(output, callback){
//Do some if else statement and do console.log
return callback(null);
}
问题:在所有迭代完成后,不会调用最终回调,即 function(err){..} 的 async.each。
【问题讨论】:
-
你的runFile被执行了吗? process.argv 也是一个空数组吗?
-
是的!一切都执行得很好,除了上面提到的最终回调之外,我从所有函数中获得了所需的输出。否
process.argv不为空(交叉检查)。 -
进入你的
//Do some if else statement and do console.log也许你不要打电话给你的回调 -
你应该在每个函数中尝试
console.log(callback),看看你是否真的在传递它 -
@Gepser 如您所见,在
//Do some if else statement and do console.log之后有一个return callback(null)。这个callback在所有if else语句之外并且在__parseOutput函数的末尾。感谢console.log(callback)的建议,但我已经尝试过了,它包含正确的callback(如果多次调用则抛出错误等...)
标签: node.js asynchronous async.js