node.js - Why async.map function works with the native fs.stat function? -
async.map(['file1','file2','file3'], fs.stat, function(err, results){ // results array of stats each file });
as per documentation, second argument is:
iterator(item, callback) - function apply each item in array.
fine.
the iterator passed callback(err, transformed) must called once has completed error (which can null) , transformed item.
i think thatfs.stat
not conform , shouldn't work.
it should like:
async.map(['file1','file2','file3'], function (file, complete) { fs.stat(file, function (err, stat) { complete(err, stat) }); }, function(err, results){ // results array of stats each file } );
fs.stat
accepts 2 parameters, first file, second callback, node convention accepts 2 parameters, error , stats of file:
fs.stat(path, callback)
which seen as
fs.stat(path, function(err, stats){ // ... });
this why works, fs.stat
called passing needs.
more info: http://nodejs.org/api/fs.html#fs_fs_stat_path_callback
Comments
Post a Comment