简体   繁体   中英

async.each callback after finishing all iterations

I have been banging my head against this piece of code for a few hours now. Moreover I have used " async.each " multiple times before. May be the code just needs a different set of eyes to work. Here's the code and the problem.

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();
        }
    }
});

runFile:

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);
}

Problem: The final callback ie function(err){..} of async.each is not called after all the iterations have finished.

All the problem was being caused by an asynchronous method being called in between callbacks. I waited for this problematic function to complete and then called the callback . In my case I used async.waterfall to wait as I had to pass values from one to the other. For similar problems, please read the comments of this question and specifically those by @Gepser .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM