title: Async Recursive Readdir in Node
body: <p>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 <a href="https://stackoverflow.com/a/5827895/1903619">this answer</a>. </p>
  
  <pre><code>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 &amp;&amp; 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);
          }
        });
      });
    });
  };
  </code></pre>
  
  <p>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?</p>
  
  <p>Something more like:</p>
  
  <pre><code>walk(path,function(files) {
    // do stuff with each file as its found
  });
  </code></pre>
  
  <p>EDIT: once I start getting the files, I plan to access them and use the <code>async</code> module to process them and prevent using up file descriptors. So something like this:</p>
  
  <pre><code>walk(path,function(files) {
      async.each(files,function() {
          // do more stuff
      }
  });
  </code></pre>
  
  <p>Will that work okay with an asynchronous scan?</p>