跳出 javascript 嵌套的 async.each 循环,但继续主循环

Posted

技术标签:

【中文标题】跳出 javascript 嵌套的 async.each 循环,但继续主循环【英文标题】:Break out of javascript nested async.each loop but continue main loop 【发布时间】:2014-06-28 06:58:22 【问题描述】:

我有一个名为 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
);

【问题讨论】:

【参考方案1】:

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

例子:

// 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 模块方法时,我实际上使用了这种模式。【参考方案2】:
// 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.
  );

【讨论】:

以上是关于跳出 javascript 嵌套的 async.each 循环,但继续主循环的主要内容,如果未能解决你的问题,请参考以下文章

在Java中,如何跳出当前的多重嵌套循环?

在JAVA中如何跳出当前的多重嵌套循环?

在java中,如何跳出当前的嵌套循环

跳出嵌套循环

如何跳出嵌套语句之return

多层嵌套break跳出指定循环