简体   繁体   中英

callback function in .each() method for Async package in Node.js

In the document of Async package, each() method takes 3 argument each(coll, iteratee, callback) . My question is not about the 3rd argument callback , but the another "callback" function in the 2nd argument iteratee .

It says iteratee is a type of AsyncFunction() function, which also takes a callback function as the argument. Here is the example provided in the document.

// assuming openFiles is an array of file names
async.each(openFiles, function(file, callback) {

  // Perform operation on file here.
  console.log('Processing file ' + file);

  if( file.length > 32 ) {
    console.log('This file name is too long');
    callback('File name too long');
  } else {
    // Do work to process file here
    console.log('File processed');
    callback();
  }
}, function(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');
  }
});

In this example, the second argument function(file, callback) should be the iteratee function. However, I do not understand where and what its callback argument is defined. It has been called in the above example with callback('File name too long'); and callback('File name too long'); but what exactly this function does? By intuition, this function might be called when the file is done processing to notify this fact. But where I could find the exact code of this callback function?

我相信您可以通过iteratorCallback函数找到此处定义的回调。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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