在循环中调用异步函数时,Node.JS如何处理循环控制?

时间:2013-01-14 01:44:17

标签: node.js loops asynchronous

我的情况如下:有一个IP地址数组。我将测试每个IP以连接到远程服务器。如果一个IP连接,其余的IP将被忽略,不会连接。

我使用以下Node.JS代码来完成工作,但似乎无法正常工作。请给出一些提示。谢谢!

// serverip is a var of string splitted by ";", e.g. "ip1;ip2;ip3"
var aryServerIP = serverip.split(";");
console.log(aryServerIP);

var ipcnt = aryServerIP.length; // ipcnt = 3, for example
for (ip in aryServerIP)
{
    console.log("to process: " + ipcnt); // error here: always print 3
    var net = require('net');
    var client = new net.Socket();
    var rdpport = 3389;
    client.connect(rdpport, aryServerIP[ip], function(){
        console.log("socket connected to " + aryServerIP[ip] + ":" + rdpport);
        client.destroy();
        if (0 != ipcnt)
        {
            // do some real connection work about aryServerIP[ip].
            ipcnt--;
        }
    });
    client.on('error', function(){
        console.log("fail to connect to " + aryServerIP[ip] + ":" + rdpport);
        ipcnt--;
    });
}

我知道使用ipcnt计数来控制循环是坏的,但我不知道如何控制Node.JS循环,当循环中调用异步函数时...

1 个答案:

答案 0 :(得分:1)

因为您的connecterror回调都是异步的,所以它们都会在for循环完成后运行。

您需要做的是设置一组回调。例如,不是使用for循环,而是将整个循环体包裹在函数中。如果连接成功,那么只需执行您通常会执行的操作,如果调用了error回调,则再次执行包装功能。像这样:

var async = require('async');
var net = require('net');
var rdpport = 3389;

function tryConnections(aryServerIP, callback){
  function connect(i){
    if (i === aryServerIP.length) return callback();

    var client = new net.Socket();
    client.connect(rdpport, aryServerIP[i], function(){
      callback(client);
    });
    client.on('error', function(){
      connect(i + 1)
    });
  }
  connect(0)
}

tryConnections(serverip.split(";"), function(client){
  if (client) // Successfully connected to something
  else // all ips failed
});

另一种解决方案是使用Async库。

function tryConnections(aryServerIP, callback){
  async.detectSeries(aryServerIP, function(ip, cb){
    var client = new net.Socket();
    client.connect(rdpport, ip, function(){
      cb(client);
    });
    client.on('error', function(){
      cb();
    });
  }, callback);
}