简体   繁体   English

如何在Async库的“Each”函数中定义回调?

[英]How do I define the callback in the Async library's “Each” function?

In Caolan's Async library, the documentation says that the each function: 在Caolan的Async库中,文档说明了each函数:

Applies the function iteratee to each item in arr, in parallel. 将函数iteratee并行应用于arr中的每个项目。 The iteratee is called with an item from the list, and a callback for when it has finished. 使用列表中的项目调用iteratee,并在完成时调用它。

This example is given: 给出了这个例子:

async.each(arr, iteraree, function(err){
    //Do stuff
});

My question is how is the "callback", in this case iteraree , defined? 我的问题是如何定义“回调”,在这种情况下是iteraree How do you declare which callback function you would like called when it has finished? 如何在完成后声明要调用哪个回调函数?

If you were able to define functions as a parameter I could see it working but you can't (ie): 如果你能够将函数定义为参数我可以看到它工作,但你不能(即):

async.each(openFiles, function (item, function () {console.log("done")}), function(err){
  //Do stuff
});

But how could I define that callback not inline? 但是我如何定义该回调不是内联的呢?

If I do something like 如果我做的事情

var iteraree = function (item, callback) {};

And passed in iteraree I still do not see where I would be able to define the behavior of callback . 并且在iteraree传递,我仍然没有看到我能够定义callback行为的位置。

That iteratee function doesn't have the signature you think it does. 该iteratee函数没有您认为的签名。 It looks like this: 它看起来像这样:

async.each(openFiles, function (item, cb){
       console.log(item);
       cb();   
    }, function(err){
      //Do stuff
 });

It receives each item and a callback function from asynchronous.each() that has to be called when you finish whatever you are doing. 它接收来自asynchronous.each()的每个项目和一个回调函数,当你完成你正在做的事情时必须调用它。 You don't provide the callback. 您不提供回调。

So if you are using a named function it's just: 因此,如果您使用的是命名函数,它只是:

 function foo (item, cb){
       console.log(item);
       cb();   
    }

async.each(openFiles, foo, function(err){
      //Do stuff
 });

I still do not see where I would be able to define the behavior of callback . 我仍然没有看到我能够定义callback的行为。

You don't. 你没有。 callback is just a parameter. callback只是一个参数。 async.each calls iteratee and passes a value for callback . async.each调用iteratee并传递一个值用于callback It's your responsibility to call callback when you are done. 完成后调用 callback是你的责任。 This lets async.each know that it can continue to the next iteration. 这让async.each知道它可以继续下一次迭代。


function (item, function () {console.log("done")}) is invalid JavaScript btw. function (item, function () {console.log("done")})是无效的JavaScript btw。 You cannot have a function definition in place of a parameter. 您不能使用函数定义来代替参数。

Here's a modified version of example from their docs with added comments 以下是来自其文档的示例的修改版本以及添加的注释

//I personally prefer to define it as anonymous function, because it is easier to see that this function will be the one iterated
async.each(openFiles, function(file, callback) {

    callback(); //this is just like calling the next item to iterate

}, then);

//this gets called after all the items are iterated or an error is raised
function then(err){
    // if any of the file processing produced an error, err would equal that error
    if( err ) {
      // One of the iterations produced an error.
      // All processing will now stop.
      console.log('A file failed to process');
    } else {
      console.log('All files have been processed successfully');
    }
}

As the following code you can see, it is a built-in function and gives us ability to throw errors according to customize limits. 如您可以看到的以下代码,它是一个内置函数,使我们能够根据自定义限制抛出错误。 All its details are controlled by async module, and we should not to override it. 它的所有细节都由异步模块控制,我们不应该覆盖它。 It is a such kind of function: you will need use it, and you no need to define it. 它是一种这样的功能:你需要使用它,你不需要定义它。

const async = require('async');

const array = [1, 2, 3, 4, 5];

const handleError = (err) => {
    if (err) {
        console.log(err);
        return undefined;
    }
}

async.each(array, (item, cb) => {
    if (item > 2) {
        cb('item is too big');
    }
    else {
        console.log(item);
    }
}, handleError);

console log: 控制台日志:

1
2
item is too big

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

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