title: Add additional parameters to callback function
body: <p>I'm building a system in Node.js that is supposed to find all files in an array of folders, stat them, and then do some additional work using that information.</p>
  
  <p>I'm using fs.readdir() to get all the files synchronously from each of the folders.  My code looks like this: </p>
  
  <pre><code>for(i=0,max=paths.length; i&lt;max; i++) {
      var path = paths.pop();
      console.log("READING PATH: " + path);
      fs.readdir(path, function(err, files) { handleDir(err, files, path); });
  }
  </code></pre>
  
  <p>The problem is that, depending on how fast the readdir() executes, handleDir() is getting the wrong path.  This happens because by the time the callback runs, the next loop has already started - meaning that the path variable has changed.</p>
  
  <p>So, what I need to do is somehow lock that path variable to it's specific callback function.  I can't think of any good way to do this - anyone have some ideas?</p>