title: Passing Additional Arguments into a Callback Function
body: <p>I feel like I must be missing something obvious, but I can't seem to find it.</p>
  
  <p>In nodejs how can I pass additional arguments into a callback function?</p>
  
  <pre><code>fs = require('fs');
  
  for(var i = 0; i &lt; 5; i++){
      fs.readFile(filePath, function(err, data){
          console.log(i);
      });
  }
  </code></pre>
  
  <p>doing it this way you usually get a bunch of 4's written to the screen. I understand why that is happening. It is happening because that is the value of 'i' by the time it gets to that part of the code for all the callbacks. This doesn't work either:</p>
  
  <pre><code>fs = require('fs');
  
  for(var i = 0; i &lt; 5; i++){
      fs.readFile(filePath, callback(err, data, i));
  }
  
  function callback(err, data, i){
      console.log(i);
  }
  </code></pre>
  
  <p>as 'err' and 'data' are undefined. I also understand why that is the case.</p>
  
  <p>This works for 'i' but then I lose the err and data values that I need.</p>
  
  <pre><code>fs = require('fs');
  
  for(var i = 0; i &lt; 5; i++){
      fs.readFile(filePath, callback(i))
  }
  
  function callback(num){
      console.log(num);
  }
  </code></pre>
  
  <p>How do I solve this?</p>