0

I would like to pass some variable to the anonymous function that is called by http.get.

Here is an example of code with the issue.

var url = require('url');
var http = require('http');

for (var i = 0, len = 3; i < 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);
        });
    });
}

What I would like in the console is something like

test0
test1
test2
test3

but what I get is

test3
test3
test3
test3

Thanks and regards,

Eric

Eric
  • 557
  • 3
  • 18

0 Answers0