简体   繁体   English

async.each-回调函数是什么?

[英]async.each - what's the callback for?

I just want to check a list of urls to see if each exists and continue after all have been done: 我只想查看网址列表,以查看每个网址是否存在,并在完成所有步骤后继续:

var urls = [ "http://...", "http://...", ... ];

async.each(urls, function(url, ??callback??) {
             http.get(url, function(response) {
                 console.log(response.statusCode);
             });
           }, function(err) {
                if (!err)
                   console.log("All urls called");
           });

What do I put for ??callback?? 我该为什么做??callback?? when I have nothing to continue after each task is completed? 当每个任务完成后我什么都没继续的时候? The documentation doesn't indicate it is optional. 文档没有表明它是可选的。

The second parameter is iteratee , which is an AsyncFunction : 第二个参数iteratee ,它是一个AsyncFunction

AsyncFunction() AsyncFunction()

An "async function" in the context of Async is an asynchronous function with a variable number of parameters, with the final parameter being a callback. 在Async上下文中,“异步函数”是具有可变数量参数的异步函数,最后一个参数是回调。 ( function (arg1, arg2, ..., callback) {} ) The final callback is of the form callback(err, results...) , which must be called once the function is completed. function (arg1, arg2, ..., callback) {} )最终的回调形式为callback(err, results...) ,一旦函数完成,就必须调用它。 The callback should be called with a Error as its first argument to signal that an error occurred. 回调应以Error作为其第一个参数来调用,以表示发生了错误。 Otherwise, if no error occurred, it should be called with null as the first argument, and any additional result arguments that may apply, to signal successful completion. 否则,如果未发生错误,则应使用null作为第一个参数以及可能适用的任何其他结果参数来调用,以表示成功完成。 The callback must be called exactly once, ideally on a later tick of the JavaScript event loop. 回调函数必须恰好被调用一次,最好是在JavaScript事件循环的稍后时刻进行。

It's an argument async calls your iteratee with. 这是一个async调用您的iteratee的参数。 In your case, you need to call it when the get is complete (successful or failed): 对于您的情况,您需要在get完成(成功或失败)时调用它:

var urls = [ "http://...", "http://...", ... ];
async.each(urls, function(url, callback) {
    http.get(url, function(response) {
        console.log(response.statusCode);
        callback(null); // <====
    });
}, function(err) {
    if (!err)
        console.log("All urls called");
});

async needs to know the operation has completed so it can manage the overall process. async需要知道操作已经完成,以便可以管理整个过程。

My example above is simplistic, you may want to differentiate success from failure, but the idea is that you must call the callback you receive, either with an error (first arguments) or success (first argument = null , optional second argument). 上面的示例很简单,您可能希望将成功与失败区分开来,但是您的想法是必须使用错误(第一个参数)或成功(第一个参数= null ,可选的第二个参数)调用收到的回调。

You're meant to call the callback when you've completed each async task, ie getting the URL. 完成每个异步任务(即获取URL)后,您应该调用callback

So something like... 所以像...

http.get(url, function(response) {
  // Here you call it with `null` to signify a non-error completion.
  callback(null);
});

...inside of your main each function. ...在您主要的每个功能内。 If all you wanted to do was call it without any further code, you could use http.get(url, callback.bind(null, null)) . 如果您只想调用它而无需任何其他代码,则可以使用http.get(url, callback.bind(null, null))

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

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