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
Post a Comment