Break out of javascript nested async.each loop but continue main loop

后端 未结 2 1062
庸人自扰
庸人自扰 2021-01-06 17:08

I have an array of arrays of objects called recipesArray.

recipesArray = [  [{name = \"the recipe name\", url = \"http://recipeurl.com\"},
                           


        
2条回答
  •  花落未央
    2021-01-06 17:46

    // 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 have 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.
      });
    

提交回复
热议问题