1

I'm working with the parallel function in Async.js and for some reason the final call back is not getting executed and I do not see an error happening anywhere.

I'm dynamically creating an array of functions that are passed to the parallel call as such:

// 'theFiles' is an array of files I'm working with in a code-generator style type of scenario
var callItems = [];
theFiles.forEach(function(currentFile) {

      var genFileFunc = generateFileFunc(destDir + "/" + currentFile, packageName, appName);

      callItems.push(genFileFunc(function(err, results) {
        if(err) {
          console.error("*** ERROR ***" + err);
        } else {
          console.log("Done: " + results);  
        }

      }));

    });

    async.parallel(callItems, function(err, results) {
      console.log(err);
      console.log(results);
      if(err) {
        console.error("**** ERROR ****");
      } else {
        console.log("***** ALL ITEMS HAVE BEEN CALLED WITHOUT ERROR ****");  
      }
    });

Then in an outside function (outside of the function that is executing the forEach above) I have the generateFileFunc() function.

// Function that returns a function that works with a file (modifies it/etc). 
function generateFileFunc(file, packageName, appName) {
  return function(callback) {
    generateFile(file, packageName, appName, callback);
  }
}

I've looked at this SO post and it helped me get to where I'm at. However the final call back is not being executed. All of the items in the parallel call are being executed though. Inside of gnerateFile (function) at the very bottom I call the callback, so thats golden.

Anyone have any idea why this might not be executing properly?

The end result is to work with each function call in parallel and then be notified when I'm done so I can continue executing some other instructions.

Thanks!

3 Answers 3

4

Analyze what is happening line by line, starting with this:

var genFileFunc = generateFileFunc(...);

Since your function generateFileFunc returns function, so variable genFileFunc is a following function

genFileFunc === function(callback) {
    generateFile( ... );
};

Now it is clear that this function returns nothing ( there is no return statement ). And obviously by nothing I understand JavaScript's built-in undefined constant. In particular you have

genFileFunc(function(err, results) { ... } ) === undefined

which is the result of calling it. Therefore you push undefined to callItems. No wonder it does not work.

It is hard to tell how to fix this without knowing what generateFile exactly does, but I'll try it anyway. Try simply doing this:

callItems.push(genFileFunc);

because you have to push function to callItems, not the result of the function, which is undefined.

Sign up to request clarification or add additional context in comments.

1 Comment

You nailed it. For some reason I kept telling myself that I was returning a function in the generateFileFunc call. But, as you stated , on the array push, I was executing the method instead of passing the function as a parameter. Great explanation.
2

Curious.

Best guess so far: Inside generateFile, RETURN callback instead of calling it.

1 Comment

Josh - You were right. However, it did not click until I followed the logic that freakish had posted. Still upvoted you though. Thanks!
1

You can achieve the stated goal with

async.map(theFiles, function(file, done) {
  generateFile(destDir + "/" + file, packageName, appName, done);
}, function(err, res) {
  // do something with the error/results
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.