使用Javascript - 等待一些异步回调的回归? [英] Javascript - waiting for a number of asynchronous callbacks to return?
本文介绍了使用Javascript - 等待一些异步回调的回归?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是用于处理多个异步回调的最佳方法/库?现在,我有这样的事情:
What's the best way/library for handling multiple asynchronous callbacks? Right now, I have something like this:
_.each(stuff, function(thing){
async(thing, callback);
});
我需要执行一些code中的回调已经在的东西被解雇的每个元素后
。
什么是做最彻底的方法?我打开使用库。
What's the cleanest way to do this? I'm open to using libraries.
推荐答案
有一个名为 Async.js 大图书馆帮助解决许多异步和放像这样的问题;流量控制助手。它提供了多种功能的forEach,可以帮助你在一个数组/对象中的每个项目上运行的回调。
There is a great library called Async.js that helps solve problems like this with many async & flow control helpers. It provides several forEach functions that can help you run callbacks for every item in a an array/object.
退房日期:
https://github.com/caolan/async#forEach
// will print 1,2,3,4,5,6,7,all done
var arr = [1,2,3,4,5,6,7];
function doSomething(item, done) {
setTimeout(function() {
console.log(item);
done(); // call this when you're done with whatever you're doing
}, 50);
}
async.forEach(arr, doSomething, function(err) {
console.log("all done");
});
这篇关于使用Javascript - 等待一些异步回调的回归?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文