javascript 如何打破 async.js 每个循环?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14939050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to break async.js each loop?
提问by mithunsatheesh
Hi i am using asyncmodule of node.js for implementing a for loop asynchronously.
嗨,我正在使用node.js 的异步模块来异步实现 for 循环。
My question is: how to break the loop execution and get out of the loop? I tried giving return
, return false
but no luck.
我的问题是:如何中断循环执行并跳出循环?我尝试给予return
,return false
但没有运气。
Here is the sample code:
这是示例代码:
async.until(
function() { return goal; },
function(callback) {
async.each(_rules,
function(rule,callback) {
var outcome = true;
.... some code ....
if(changes){
console.log("hi");
return false;// HERE I NEED TO BREAK
}
else
callback();
},
function(err){ }
);
if (!changes || session.process)
goal = true;
callback();
},
function(err){ callback(session); }
);
回答by user568109
async.until
repeatedly calls function until the test returns true. So test must return true so that you exit the loop. This is opposite of async.whilst
which runs repeatedly while test evaluates to be true.
async.until
重复调用函数,直到测试返回 true。因此 test 必须返回 true 以便您退出循环。这与async.whilst
在 test 评估为真时重复运行相反。
async.each
calls the functions in parallel so what it returns does not matter. It is not a loop which you can break, but an iterator looping over the array. Your condition to stop using async.each
should be in test for async.until
and you should iterate over the rules yourself.
async.each
并行调用函数,所以它返回什么并不重要。它不是一个可以中断的循环,而是一个遍历数组的迭代器。您停止使用的条件async.each
应该在测试中async.until
,您应该自己迭代规则。
回答by AndyD
There isn't really a "loop" as such to break out of. All your items in your collection are used in parallel
没有真正的“循环”可以打破。您收藏中的所有物品都并行使用
The only way to "break" the "loop" is to call the callback with an error argument. As there is nothing to stop you from putting other things in there you could hack it a little bit.
“中断”“循环”的唯一方法是使用错误参数调用回调。由于没有什么可以阻止您将其他东西放入其中,因此您可以稍微修改一下。
From the docs:
从文档:
Note, that since this function applies the iterator to each item in parallel there is no guarantee that the iterator functions will complete in order.
请注意,由于此函数将迭代器并行应用于每个项目,因此不能保证迭代器函数将按顺序完成。
Even if you return an error, you will still have several outstanding requests potentially so you really want to limit the amount of items you use in one go. To limit the amount of outstanding requests, you could use eachSeries or eachLimit.
即使你返回一个错误,你仍然可能有几个未完成的请求,所以你真的想限制你一次性使用的项目数量。要限制未完成请求的数量,您可以使用 eachSeries 或 eachLimit。
For example:
例如:
async.each(_rules,
function(rule,callback) {
if(changes){
return callback({ data: 'hi'}); // stop
}
...
if(realerror){
return callback({ error: realerror}); // stop with an error
}
callback(); // continue
},
function(result){
if(!result){
return othercallback('no results');
}
// check if we have a real error:
if(result.error){
return othercallback(result.error);
}
return othercallback(null, result.data);
}
);
PS: if you're not doing async, use underscore
PS:如果你不做异步,请使用下划线