node.js - Identify event emitter from multiple -


i trying create multiple http requests in node.js, each of them receiving separate response. want identify event corresponds call:

for (var i=0; i<100; i++) {     var req = http.request(options, function(response) {         var str = "";          response.on('data', function (chunk) {             console.log(str.length);         });          response.on('end', function () {             console.log("end of response");         });       });       req.on('error', function(err) {         console.log(err.message);     });      req.end(); } 

is there way of identifying response corresponds each iteration? creating 100 response instances, emit same event, event emitting/handling done globally. basically, somehow tie i , events emitted response?

@benfortune right, related closures. example in original question overly-simplified, if have construction similar to:

for(var i=0; ... ) {      function somefunction() {      }  } 

and want keep track of external function inside function, should closures.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -