I have the following Node.js code:
var exec=require('child_process').exec;
var base_ctrl_port=8118;
var base_data_port=9050;
var nConns=10;
watchdogCycle();
setInterval(watchdogCycle, 60000);   //check health every 60 seconds...
function watchdogCycle(){
        console.log("\n");
        for(var i=0;i<nConns;i++){
                var data_port=base_data_port+i;
                data_port=data_port+"";
                var curl=exec('curl -b -s --socks5 localhost:'+data_port+' http://ifconfig.me',{timeout:10000},
                function(err,stdout,stderr){
                        console.log(stdout);
                        if(err!=null){
                                getNewIP(i);   //PROBLEM: i is always 10!!!
                        }
                });
        }
}
function getNewIP(offset){
        console.log("Getting new IP address for tor data port: "+(base_data_port+offset+0)+"...");
        var ctrl_port=base_ctrl_port+offset;
        var nc=exec('(echo AUTHENTICATE \'\"xxxxxx\"\'; echo SIGNAL NEWNYM; echo quit) | nc localhost '+ctrl_port,
        function(err,stdout,stderr){
                console.log(stdout);
        });
}
The problem is that the parameter, i, into getNewIP(i) is always 10!
I've read up a bit about recursion, but I don't know how to modify this code so that i is 0..9 and not always 10.
Many thanks in advance,
 
     
     
     
    