I'm trying to understand how http.get is being called here. So here is a sample code I'm experimenting with:
var http = require('http');
var urls = ['http://www.google.com',
'http://www.yahoo.com',
'http://www.cnn.com']
for (i = 0; i < 3; i++ ){
url = urls[i]
console.log(url)
console.log('------')
http.get(url, function(resp){
console.log(url)
});
};
Code above gives me this output:
http://www.google.com
------
http://www.yahoo.com
------
http://www.cnn.com
------
http://www.cnn.com
http://www.cnn.com
http://www.cnn.com
I got the first three (the ones with before '------'). What i don't understand is, why did the last three lines all give me cnn.com? i thought the http.get request kicks off with each iteration?
Does it mean console.log(url) inside http.get is referring to the url=urls[2] outside http.get when 'for' reaches the end of the loop? How do i print the url, aka the argument passed into http.get?