Created
July 9, 2015 13:05
-
-
Save sethblanchard/26f81fcbc896b8a2f7ad to your computer and use it in GitHub Desktop.
Read in several files and once they are all in memory do something with them
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var data_dir = '/data/', | |
read_dir = __dirname + data_dir; | |
var readFilesThenDoSomething = function(files, callback){ | |
var completed_files = []; //could error check here | |
files.map(function(file_name){ | |
var file_contents = ''; | |
var stream = fs.createReadStream(read_dir + file_name, {encoding: 'utf8'}); | |
stream.on('data', function(chunk){ | |
file_contents += chunk; //we assume everything is a string... | |
}) | |
stream.on('end', function(){ | |
completed_files.push(file_contents); | |
if(completed_files.length == files.length){ | |
callback(completed_files); | |
} | |
}); | |
}); | |
}; | |
fs.readdir(read_dir, function(err, files){ | |
if(err) return; | |
readFilesThenDoSomething(files, function(completed){ | |
console.log('we can do something with all the %d files!', completed.length); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment