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());
}
});
});
}
2 Answers 2
I believe you can shorten it to just
function testAsync() {
var files = ['Statistics_Schema.json', 'seriesData'];
async.map(files, fs.readFile, function (err, data) {
for(var i = 0, l = data.length ; i < l ; i++) {
console.log( data[i].toString() );
}
});
}
Right now, you're first calling fs.stat
on each file, only so you can create a buffer in your fileread
method.
But fs.readFile
does not take a buffer argument. It takes an options object (and there's no buffer option).
The Buffer
object you're passing is an object, though, so Node doesn't complain. It just doesn't find any relevant fileRead
options in that object either, so it's basically ignored.
So you don't need the buffer, which means you don't need fs.stat
, which means you can skip almost all of your code.
Why not using fs.readFile
? I just tested this code:
var async = require('async'),
fs = require('fs'),
files = ['file1.json', 'file2.json'];
async.map(files, fs.readFile, function(err, files) {
if(err) {
throw err;
}
files.forEach(console.log);
});
If you wanna be able to read the file contents then replace the last line with
files.forEach(function(file) {
console.log(file.toString());
});
-
\$\begingroup\$ He is using
fs.readFile
infileread
with a buffering option. \$\endgroup\$konijn– konijn2014年08月13日 13:33:54 +00:00Commented Aug 13, 2014 at 13:33 -
1\$\begingroup\$ Hi @konjin - I thought that with both codes he would achieve the same results, then I googled about the difference in passing a buffer to
readFile
and could find nothing on the docs. Can you explain the difference? \$\endgroup\$Renato Gama– Renato Gama2014年08月13日 13:45:08 +00:00Commented Aug 13, 2014 at 13:45 -
\$\begingroup\$ Interesting, it seems you are correct, passing
buffer
toreadFile
does not make any sense. \$\endgroup\$konijn– konijn2014年08月13日 13:49:42 +00:00Commented Aug 13, 2014 at 13:49 -
\$\begingroup\$ Also, see this: codereview.stackexchange.com/a/59783/14625 \$\endgroup\$konijn– konijn2014年08月13日 13:50:18 +00:00Commented Aug 13, 2014 at 13:50
-
1\$\begingroup\$ At the time I answered @Flambino's answer wasn't visible to me, I believe its due to the migration from SO. Pretty much the same answer. \$\endgroup\$Renato Gama– Renato Gama2014年08月13日 15:31:24 +00:00Commented Aug 13, 2014 at 15:31
Explore related questions
See similar questions with these tags.
promise
pattern. Recommend to read: strongloop.com/strongblog/… \$\endgroup\$