0

这似乎有效:

function callLoop (n) {
  function caller () {
    console.log("hello from " + n);
    setTimeout(function () {
      caller();
    }, 10000);
  }

  caller();
}

for (var i = 0; i < 5; i++) {
  callLoop(i);
}

在此示例中,setTimeout 将改为长时间运行的网络调用。这是并行化这些网络调用的“正确”方法吗?

4

1 回答 1

0

签出async.parallel

var async = require( 'async' );

function callLoop (n) {
  function caller () {
    console.log("hello from " + n);
    setTimeout(function () {
      caller();
    }, 10000);
  }

  caller();
}

var functions = [];
for (var i = 0; i < 5; i++) {
  functions.push(callLoop.bind(null, i));
}

async.parallel(functions);
于 2013-08-23T20:48:35.330 回答