原文链接: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 inarr. [The iterator is passed acallback(err, transformed)which must be called once it has completed with an error (which can benull) and a transformed item.]callback(err, results)- Optional A callback which is called when alliteratorfunctions have finished, or an error occurs. Results is an array of the transformed items from thearr.
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])
本文详细介绍了Node.js中map方法的使用,包括其核心参数、工作原理及应用示例,帮助开发者理解如何通过并行处理数组元素来优化代码效率。





