async.each - 回调的目的是什么?
async.each - what's the callback for?
我只想检查一个 url 列表,看看是否每个都存在,并在完成所有操作后继续:
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");
});
当我完成每项任务后无事可做时,我应该为 ??callback??
添加什么? documentation 并不表示它是可选的。
您应该在完成每个异步任务后调用 callback
,即获取 URL。
所以像...
http.get(url, function(response) {
// Here you call it with `null` to signify a non-error completion.
callback(null);
});
...在您的主要 each 函数中。如果您只想调用它而无需任何其他代码,则可以使用 http.get(url, callback.bind(null, null))
.
second parameter is iteratee
, which is an 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. (function (arg1, arg2, ..., callback) {}
) The final callback is of the form callback(err, results...)
, which must be called once the function is completed. The callback should be called with a Error
as its first argument to signal that an error occurred. 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. The callback must be called exactly once, ideally on a later tick of the JavaScript event loop.
这是一个参数 async
调用您的 iteratee
的参数。在您的情况下,您需要在 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
需要知道操作已完成,以便它可以管理整个过程。
我上面的例子很简单,你可能想区分成功和失败,但想法是你必须调用你收到的回调,要么是错误(第一个参数)要么是成功(第一个参数 = null
, 可选的第二个参数).
我只想检查一个 url 列表,看看是否每个都存在,并在完成所有操作后继续:
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");
});
当我完成每项任务后无事可做时,我应该为 ??callback??
添加什么? documentation 并不表示它是可选的。
您应该在完成每个异步任务后调用 callback
,即获取 URL。
所以像...
http.get(url, function(response) {
// Here you call it with `null` to signify a non-error completion.
callback(null);
});
...在您的主要 each 函数中。如果您只想调用它而无需任何其他代码,则可以使用 http.get(url, callback.bind(null, null))
.
second parameter is iteratee
, which is an 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. (
function (arg1, arg2, ..., callback) {}
) The final callback is of the formcallback(err, results...)
, which must be called once the function is completed. The callback should be called with aError
as its first argument to signal that an error occurred. 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. The callback must be called exactly once, ideally on a later tick of the JavaScript event loop.
这是一个参数 async
调用您的 iteratee
的参数。在您的情况下,您需要在 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
需要知道操作已完成,以便它可以管理整个过程。
我上面的例子很简单,你可能想区分成功和失败,但想法是你必须调用你收到的回调,要么是错误(第一个参数)要么是成功(第一个参数 = null
, 可选的第二个参数).