I'm trying to identify the resolved promises in a loop using q module in NodeJS.
var portscanner = require('portscanner');
var Q = require('q');
var portScan1 = function(ip) {
return portscanner.checkPortStatus(22, ip);
}
var portScan2 = function(ip) {
return portscanner.checkPortStatus(80, ip);
}
for(var i=1; i<=254; i++) {
var ip = '10.10.1.' + i;
Q.all([portScan1(ip), portScan2(ip)]).then(function(resolve) {
console.dir({ ip: ip, func1result: resolve[0], func2result: resolve[1] });
});
}
The problem is that the variable i goes to 99 until the first Q is resolved.
{ ip: '10.10.1.254', func1result: 'open', func2result: 'closed' }
{ ip: '10.10.1.254', func1result: 'closed', func2result: 'closed' }
{ ip: '10.10.1.254', func1result: 'closed', func2result: 'open' }
{ ip: '10.10.1.254', func1result: 'closed', func2result: 'open' }
... (and so on)
How can I resolve this issue so that I can identify the id for the resolved promise?
Thx