0

I have an asynchronous function inside a for loop nested in another for loop.

// recipesArray is an array of arrays of objects
// recipeObject is an array of objects
// currentRecipe is an object

connectToDb(function(){
   // LOOP 1
   for (var i=0, l=recipesArray.length; i < l; i++) {
      // recipeObject is an 
      var recipeObject = recipesArray[i];

      // LOOP 2
      for (var x=0, y=recipeObject.length; x < y; x++) {
         var currentRecipe = recipeObject[x];

         // this is an asynchronous function
         checkRecipe(currentRecipe, function (theRecipe) {
            if (theRecipe === undefined) {
               console.log('RECIPE NOT FOUND');
            } else {
               console.log('RECIPE FOUND', theRecipe);
            }
         });
      }
   }
});

I need to add data to the recipesArray based on the results of the checkRecipe function.

I've been trying different things... - do i try to keep track of i and x... - do i try to have multiple callbacks... - do i even need to do all of that, or is there some other way....

I also tried using the async library for node(which actually has been very helpful with other situations), but the forEach doesn't take objects(only an array).

Stuck.

Any suggestions would be greatly appreciated.

11
  • You say that forEach only works on arrays, but you have only arrays? Or at least recipesArray and recipeObject both look very array-like. Commented Apr 28, 2014 at 2:45
  • Um, those objects are passed as arguments to your callback function? Maybe show us the code you've tried. Commented Apr 28, 2014 at 2:59
  • If I pass something like this into the async.forEach: [{ color: red, size: med }] I wont get access to color and size in the each function. Commented Apr 28, 2014 at 3:07
  • @Bergi, I think this is the issue I'm running into: github.com/caolan/async/issues/26 Commented Apr 28, 2014 at 3:14
  • 1
    try not to "edit" the original object, but rather use map instead of each. And yes, i and x are automatically kept for you in the closure when you use a callback. Commented Apr 28, 2014 at 3:29

1 Answer 1

2

Assuming checkRecipe() can be run in parallel with no limits, here's how you might use async.each():

connectToDb(function() {
  async.each(recipesArray, function(subArray, callback) {
    async.each(subArray, function(currentRecipe, callback2) {
      checkRecipe(currentRecipe, function(theRecipe) {
        if (theRecipe === undefined)
          return callback2(new Error('Recipe not found'));
        callback2();
      });
    }, callback);
  }, function(err) {
    if (err)
      return console.error('Error: ' + err);

    // success, all recipes found
  });
});
Sign up to request clarification or add additional context in comments.

7 Comments

So then what does (a small example of) recipesArray look like?
[{ color: red, size: med }, { color: green, size: large }]
Ok, then you don't actually have an array of arrays then. You just have an array of objects. I've updated my answer to reflect this.
Sorry, I said that wrong(wrong array). The recipesArray looks like [ [ { key: value, key: value, key: value }, {key value..... }, {... } ], [{},{},{}] ]
an array of arrays of objects
|

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.