javascript - asynchronous http.get callbacks in Node.js -
In the node, I have this function snippet (the actual function is much less than that, so hopefully I have something important Also did not cut):
driver.protitip.update devices = function (for IP in this IP) {var curIp = ip; If (this.ips [curIp]) // // If this [CurIp] is set to undefined ... http.get ({host: curIp, port: 80, path: '/ tstat'}, on function (res) ('end', function () {// each Want to parser data) {var result = ''; res.on ('data', function (chunk) {result = IP, but // curip is always the last IP in the list});}); }; }; }; Do I have a "driver" object that includes "IP", which has a list of IP addresses, such as {"192.168.1.111": {stuff}, "192.168.1.112": {stuff}} Of course it is very clear that I am ignoring, but I hope that this can not be done to do the work, obviously, http.get () is often called asynchronous. That which I want; However, when the result is obtained and the "end" callback function is called, then I can not use the "curape" variable, which I have to include the original IP address to recall that special request. Instead the "curif" variable is always the last IP address in "this.ips". What am I missing? Any help will be very much appreciated!
curia
to to loop
The attachment updated device
function is scotched, so it is shared by all the http.get
calls and it is overwritten through every time < / Code> Loop
The typical way to fix this is to create an improvised task, which creates its own radius that captures the curif
value of each running as the parameter of that function Can:
if (this.ips [curIp]) {(function (ip) {// instant function with its own realm http.get ({host: ip, port : 80, path: '/ tstat'}, function (ridge) {var result = ''; res.on ('data', function (chuck)) {result = chuck;}); res.on ('end ', Function () {// IP is captured Took ipCur here});}); }} (CurIp); // Pass the QIR as IP parameter};
Comments
Post a Comment