async.each callback after finishing all iterations

2.2k views Asked by At

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 i.e. function(err){..} of async.each is not called after all the iterations have finished.

1

There are 1 answers

0
Gautam On

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.