title: Javascript variable scope when using http.get in node.js
body: <p>I would like to pass some variable to the anonymous function that is called by http.get.</p>
  
  <p>Here is an example of code with the issue.</p>
  
  <pre><code>var url = require('url');
  var http = require('http');
  
  for (var i = 0, len = 3; i &lt; len; i++) {
  
      var myText = "test"+i;
      var myUrl = "http://www.google.com"
  
      var options = url.parse(myUrl);
      http.get(options, function (response) {
          var chunks = [];
          response.on('data', function (chunk) {
              chunks.push(chunk);
          }).on('end', function() {
              console.log(myText);
          });
      });
  }
  </code></pre>
  
  <p>What I would like in the console is something like</p>
  
  <pre><code>test0
  test1
  test2
  test3
  </code></pre>
  
  <p>but what I get is</p>
  
  <pre><code>test3
  test3
  test3
  test3
  </code></pre>
  
  <p>Thanks and regards,</p>
  
  <p>Eric</p>