I'm trying to scan a large directory of files and sub-directories with a recursive function that I've written. It's very similar to the parallel loop given in this answer.
var fs = require('fs');
var walk = function(dir, done) {
  var results = [];
  fs.readdir(dir, function(err, list) {
    if (err) return done(err);
    var pending = list.length;
    if (!pending) return done(null, results);
    list.forEach(function(file) {
      file = dir + '/' + file;
      fs.stat(file, function(err, stat) {
        if (stat && stat.isDirectory()) {
          walk(file, function(err, res) {
            results = results.concat(res);
            if (!--pending) done(null, results);
          });
        } else {
          results.push(file);
          if (!--pending) done(null, results);
        }
      });
    });
  });
};
This issue is that it isn't really asynchronous. The whole thing processes and returns a giant array of files. Is there a way to recursively scan a directory asynchronously?
Something more like:
walk(path,function(files) {
  // do stuff with each file as its found
});
EDIT: once I start getting the files, I plan to access them and use the async module to process them and prevent using up file descriptors. So something like this:
walk(path,function(files) {
    async.each(files,function() {
        // do more stuff
    }
});
Will that work okay with an asynchronous scan?
 
     
    