title: Passing Variables to Node.JS Callback Functions
body: <p>I'm pretty new to Node.JS and I'm trying to build an app but I'm running into some issues which I can't seem to find a solution to. I spent quite a bit of time on Google with no luck.</p>
  
  <p>Below is a little snippet which illustrate what I'm trying todo.</p>
  
  <pre><code>mysql.query('SELECT * FROM leads', function(err, res){
      for(x in res){
          id = res[x];
  
          mysql.query('SELECT FROM survey WHERE lead_id = ?', [id], function(x, res){
              // I want to access the id variable here but since this is in a 
              // callback the id variable could've been changed by now
  
              // AND THIS DOES'T ALWAYS RETURN SOMETHING OR I COULD'VE 
              // JUST USED THE LEAD_ID IN THE RETURNED DATASET
          });
      }
  });
  </code></pre>
  
  <p>I'd like to access that <code>id</code> variable within the callback but due to the async nature of node the variable would've changed since the for loop finish executing before the first callback is even returned.</p>
  
  <p>My question is can you pass the variable through to the the callback function?  Or can someone help me with another solution.</p>
  
  <p>Thanks in advance!</p>
  
  <p>Ahmad</p>