且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Javascript - 等待一些异步回调的回归?

更新时间:2022-01-09 22:10:10

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