Skip to content

Instantly share code, notes, and snippets.

@ramonvictor
Created April 4, 2014 13:23
Show Gist options
  • Save ramonvictor/9974605 to your computer and use it in GitHub Desktop.
Save ramonvictor/9974605 to your computer and use it in GitHub Desktop.
Synchronous nodejs directory loop
var glob = require("glob");
var fs = require("fs");
// async map file contents
var async = function async(arg, callback) {
fs.readFile(arg, 'utf8', function (err, data) {
if (err) throw err;
callback(arg, data);
});
};
// loop directory
glob("test/**/*.js", function (er, files) {
files.forEach(function(item) {
async(item, function(filePath, data){
_this[ collection ].push(
{
path : filePath,
contents : data
}
);
if( _this[ collection ].length == files.length ) {
// finished to read all files. Then invoke callback
_cb();
}
})
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment