title: Node.JS: How to pass variables to asynchronous callbacks?
body: <p>I'm sure my problem is based on a lack of understanding of asynch programming in node.js but here goes.</p>
  
  <p>For example: I have a list of links I want to crawl. When each asynch request returns I want to know which URL it is for. But, presumably because of race conditions, each request returns with the URL set to the last value in the list.</p>
  
  <pre><code>var links = ['http://google.com', 'http://yahoo.com'];
  for (link in links) {
      var url = links[link];
      require('request')(url, function() {
          console.log(url);
      });
  }
  </code></pre>
  
  <p>Expected output:</p>
  
  <pre><code>http://google.com
  http://yahoo.com
  </code></pre>
  
  <p>Actual output:</p>
  
  <pre><code>http://yahoo.com
  http://yahoo.com
  </code></pre>
  
  <p>So my question is either:</p>
  
  <ol>
  <li>How do I pass url (by value) to the call back function? OR</li>
  <li>What is the proper way of chaining the HTTP requests so they run sequentially? OR</li>
  <li>Something else I'm missing?</li>
  </ol>
  
  <p>PS: For 1. I don't want a solution which examines the callback's parameters but a general way of a callback knowing about variables 'from above'.</p>