打破了JavaScript的嵌套async.each环路而是继续主循环 [英] Break out of javascript nested async.each loop but continue main loop
本文介绍了打破了JavaScript的嵌套async.each环路而是继续主循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有对象的数组的数组称为recipesArray。
recipesArray = [[{名=配方名,URL =http://recipeurl.com},
{NAME =其他尼姆,URL =http://adifferenturl.com},
{NAME =另一个配方,URL =http://anotherurl.com}], [{名称=配方名,URL =http://recipeurl.com},
{NAME =其他尼姆,URL =http://adifferenturl.com},
{NAME =另一个配方,URL =http://anotherurl.com}], [{名称=配方名,URL =http://recipeurl.com},
{NAME =其他尼姆,URL =http://adifferenturl.com},
{NAME =另一个配方,URL =http://anotherurl.com}]
我要打破这个嵌套async.each循环,而是继续在主async.each循环。
//主async.each
async.each(配方,功能(子数组,调用callback1){
//嵌套async.each
async.each(子阵列功能(theCurrentRecipe,callback2){
checkHREFS(theCurrentRecipe,功能(thisRecipe){
如果(我有一个条件在这里'){
//打破这个嵌套async.each的,
//而是继续主要async.each。
}其他{
//继续
}
callback2();
});
},调用callback1);
},功能(错误){
如果(ERR){
返回console.error(ERR); //成功,所有配方迭代
});
解决方案
一种方式是修改的最终回调内部各()来检查与指示你早日打破了一个特殊的属性Error对象而且它不是一个真正的错误。然后里面的条件,传递一个错误的对象,与该属性设置,回调。
例如:
//主async.each
async.each(配方,功能(子数组,调用callback1){
//嵌套async.each
async.each(子阵列功能(theCurrentRecipe,callback2){
checkHREFS(theCurrentRecipe,功能(thisRecipe){
如果(我有一个条件在这里'){
//打破这个嵌套async.each的,
//而是继续主要async.each。
VAR fakeErr =新的Error();
fakeErr.break = TRUE;
返回callback2(fakeErr);
}
//继续
callback2();
});
},功能(错误){
如果(ERR和放大器;&安培; err.break)
调用callback1();
其他
调用callback1(ERR);
});
},功能(错误){
如果(ERR)
返回console.error(ERR); //成功,所有配方迭代
});
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
});
解决方案
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
});
这篇关于打破了JavaScript的嵌套async.each环路而是继续主循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文