这似乎有效:
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将是一个长时间运行的网络调用。这是并行化这些网络呼叫的“正确”方法吗?
答案 0 :(得分:0)
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);