Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have an array of arrays of objects called recipesArray.

recipesArray = [  [{name = "the recipe name", url = "http://recipeurl.com"},
                   {name = "the other neame", url = "http://adifferenturl.com"},
                   {name = "another recipe", url = "http://anotherurl.com"}],

                   [{name = "the recipe name", url = "http://recipeurl.com"},
                   {name = "the other neame", url = "http://adifferenturl.com"},
                   {name = "another recipe", url = "http://anotherurl.com"}],

                   [{name = "the recipe name", url = "http://recipeurl.com"},
                   {name = "the other neame", url = "http://adifferenturl.com"},
                   {name = "another recipe", url = "http://anotherurl.com"}] ]

I want to break out of this nested async.each loop, but continue the main async.each loop.

// main async.each
async.each(recipes, function(subArray, callback1) {
   // nested async.each
   async.each(subArray, function(theCurrentRecipe, callback2) {
      checkHREFS(theCurrentRecipe, function(thisRecipe) {
         if ('i have a conditional here') {
            // break out of this nested async.each, 
            // but continue the main async.each.
         } else {
            // continue
         }
         callback2();
      });
   }, callback1);
}, function(err) {
if (err) {
   return console.error(err);

   // success, all recipes iterated
});
share|improve this question
up vote 4 down vote accepted

One way might be to modify the final callback for the inner each() to check for an Error object with a special property that indicates you're breaking out early and that it's not a real error. Then inside your conditional, pass an Error object, with that property set, to the callback.

Example:

// main async.each
async.each(recipes, function(subArray, callback1) {
  // nested async.each
  async.each(subArray, function(theCurrentRecipe, callback2) {
    checkHREFS(theCurrentRecipe, function(thisRecipe) {
      if ('i have a conditional here') {
        // break out of this nested async.each, 
        // but continue the main async.each.
        var fakeErr = new Error();
        fakeErr.break = true;
        return callback2(fakeErr);
      }
      // continue
      callback2();
    });
  }, function(err) {
    if (err && err.break)
      callback1();
    else
      callback1(err);
  });
}, function(err) {
  if (err)
    return console.error(err);

  // success, all recipes iterated
});
share|improve this answer
    
That seems like it would work, thanks @mscdex. – Joel May 11 '14 at 21:26
    
Although I'm still wondering if there's a better way than to fake an error. – Joel May 11 '14 at 21:27
    
Not currently with the async module, I actually use this pattern when I need to break early with async module methods. – mscdex May 11 '14 at 21:38
    
this works , thanx – Osman Erdi Dec 14 '15 at 1:01
// inner async.each (simplificated)
  async.each(subArray, function(theCurrentRecipe, callback2) {
    checkHREFS(theCurrentRecipe, function(thisRecipe) {
      if ('i have a conditional here') {
        // going to break out of this nested async.each
        return callback2({flag:true}); // It doesn't need to be an "new Error()" ;-)
      }
      // continue
      callback2();
    });
  }, function(msg) {
    if (msg && msg.flag) // Here CHECK THE FLAG
      callback1(); // all good!... we brake out of the loop!
    else
      callback1(msg); // process possible ERROR.
  });
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.