【问题标题】:async.each callback after finishing all iterations完成所有迭代后的 async.each 回调
【发布时间】: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


【解决方案1】:

所有问题都是由在回调之间调用的异步方法引起的。我等待这个有问题的功能完成,然后调用callback。就我而言,我使用async.waterfall 等待,因为我必须将值从一个传递到另一个。对于类似的问题,请阅读这个问题的 cmets,特别是 @Gepser 的那些。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 2013-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    相关资源
    最近更新 更多