async.map(['file1','file2','file3'], fs.stat, function(err, results){
// results is now an array of stats for each file
});
As per documentation, the second argument is:
iterator(item, callback) - A function to apply to each item in the array.
Fine.
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.
I think thatfs.stat
does not conform to this and I would say that this shouldn't work.
It should be something like:
async.map(['file1','file2','file3'],
function (file, complete) {
fs.stat(file, function (err, stat) {
complete(err, stat)
});
}, function(err, results){
// results is now an array of stats for each file
}
);
From the documentation at http://nodejs.org/api/fs.html#fs_fs_stat_path_callback
fs.stat(path, callback)
Asynchronous stat(2). The callback gets two arguments (err, stats) where stats is a fs.Stats object. See the fs.Stats section below for more information.
Since the fs.stat callback returns (err, stats), the following works fine
To do the same yourself pass a function which with the appropriate callback
fs.stat
accepts two parameters, the first is the file, the second is the callback, which by node convention accepts two parameters, an error and the stats of the file:which could be seen as
This is why it works,
fs.stat
is called by passing exactly what it needs.More info: http://nodejs.org/api/fs.html#fs_fs_stat_path_callback