NodeJS中使用async控制并发-@CAOLAN

原文链接:http://wiki.jikexueyuan.com/project/node-lessons/async.html


补充:

map(arr, iterator, [callback])

Produces a new array of values by mapping each value in arr through the iterator function. The iterator is called with an item from arr and a callback for when it has finished processing. Each of these callback takes 2 arguments: an error, and the transformed item from arr. If iterator passes an error to its callback, the main callback (for the map function) is immediately called with the error.

Note, that since this function applies the iterator to each item in parallel, there is no guarantee that the iterator functions will complete in order. However, the results array will be in the same order as the original arr.

Arguments

  • arr - An array to iterate over.
  • iterator(item, callback) - A function to apply to each item in arr. [The iterator is passed a callback(err, transformed) which must be called once it has completed with an error (which can be null) and a transformed item.]
  • callback(err, results) - Optional A callback which is called when all iterator functions have finished, or an error occurs. Results is an array of the transformed items from the arr.

Example

async.map(['file1','file2','file3'], fs.stat, function(err, results){
    // results is now an array of stats for each file
});

Related

  • mapSeries(arr, iterator, [callback])
  • mapLimit(arr, limit, iterator, [callback])

你可能感兴趣的:(NodeJS中使用async控制并发-@CAOLAN)