简体   繁体   English

Javascript - 等待多个异步回调返回?

[英]Javascript - waiting for a number of asynchronous callbacks to return?

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);
});

I need to execute some code after the callback has been fired for each element in stuff . 在为stuff每个元素触发回调之后,我需要执行一些代码。

What's the cleanest way to do this? 最干净的方法是什么? I'm open to using libraries. 我愿意使用库。

Since you're already using Underscore you might look at _.after . 由于您已经在使用_.after您可以查看_.after It does exactly what you're asking for. 它完全符合您的要求。 From the docs: 来自文档:

after _.after(count, function) _.after(count, function) 之后 _.after(count, function)

Creates a version of the function that will only be run after first being called count times. 创建一个只在首次被调用计数次数后才能运行的函数版本。 Useful for grouping asynchronous responses, where you want to be sure that all the async calls have finished, before proceeding. 在继续之前,您可以将异步响应分组,以确保所有异步调用都已完成。

There is a great library called Async.js that helps solve problems like this with many async & flow control helpers. 有一个很棒的库Async.js可以帮助解决许多异步和流量控制助手这样的问题。 It provides several forEach functions that can help you run callbacks for every item in a an array/object. 它提供了几个forEach函数,可以帮助您为数组/对象中的每个项运行回调。

Check out: https://github.com/caolan/async#forEach 退房: 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");
});

I recommend https://github.com/caolan/async for this. 我建议https://github.com/caolan/async You can use async.parallel to do this. 您可以使用async.parallel执行此操作。

function stuffDoer(thing) {
    return function (callback) {
        //Do stuff here with thing
        callback(null, thing);
    }
}

var work = _.map(stuff, stuffDoer)
async.parallel(work, function (error, results) {
    //error will be defined if anything passed an error to the callback
    //results will be an unordered array of whatever return value if any
    //the worker functions passed to the callback
}

async.parallel() / async.series should suit your requirement. async.parallel()/ async.series应该符合您的要求。 You can provide with a final callback that gets executed when all the REST calls succeed. 您可以提供在所有REST调用成功时执行的最终回调。

async.parallel([
    function(){ ... },
    function(){ ... }
], callback);
async.series([
    function(){ ... },
    function(){ ... }
], callback);

Have a counter, say async_count . 有一个计数器,比如async_count Increase it by one every time you start a request (inside you loop) and have the callback reduce it by one and check if zero has been reached - if so, all the callbacks have returned. 每次启动请求时(在循环内部)将其增加一,并使回调减少一,并检查是否已达到零 - 如果是,则返回所有回调。

EDIT: Although, if I were the one writing this, I would chain the requests rather than running them in parallel - in other words, I have a queue of requests and have the callback check the queue for the next request to make. 编辑:虽然,如果我是写这个的人,我会链接请求而不是并行运行它们 - 换句话说,我有一个请求队列,并让回调检查队列以进行下一个请求。

See my response to a similar question: 请参阅我对类似问题的回答:

Coordinating parallel execution in node.js 协调node.js中的并行执行

My fork() function maintains the counter internally and automatically. 我的fork()函数在内部自动维护计数器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM