Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
中断javascript嵌套async.each循环,但继续主循环_Javascript_Node.js_Loops_Asynchronous_Async.js - Fatal编程技术网

中断javascript嵌套async.each循环,但继续主循环

中断javascript嵌套async.each循环,但继续主循环,javascript,node.js,loops,asynchronous,async.js,Javascript,Node.js,Loops,Asynchronous,Async.js,我有一个名为recipesArray的对象数组 recipesArray = [ [{name = "the recipe name", url = "http://recipeurl.com"}, {name = "the other neame", url = "http://adifferenturl.com"}, {name = "another recipe", url = "http://anotherur

我有一个名为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"}] ]
我想中断这个嵌套的async.each循环,但继续主async.each循环

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

一种方法可能是修改内部each()的最终回调,以检查具有特殊属性的错误对象,该属性指示您提前爆发,并且它不是真正的错误。然后在条件中,将一个属性设置为的错误对象传递给回调

例如:

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

虽然我仍在想是否有比伪造错误更好的方法。目前在
async
模块中没有,但当我需要提前使用
async
模块方法时,我实际上使用了这种模式。
// 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.
  });