I want to read multiple files simultaneously and apply the same function on data.
This code works correctly, but can someone please suggest any better way of writing this?
function fileread(path,stats,callback){
var buffer = new Buffer(stats.size);
fs.readFile(path, buffer, function(err, data){
if(err){
throw err;
}
callback(null, data);
});
}
function testAsync() {
var files = ['Statistics_Schema.json','seriesData'];
async.map(files, fs.stat, function(err, stats){
var i=0;
async.map(files, function(path, callback){
fileread(path, stats[i++], callback);
},function(err, data){
for(var j=0; j < data.length; j++){
console.log(data[j].toString());
}
});
});
}
promise
pattern. Recommend to read: strongloop.com/strongblog/… – Junle Li Aug 12 at 5:19