在示例中:
async.eachLimit([1,2,3,4,5,6,7,8,9,10], 6, function(a, b) {
console.log("current: " + a);
b("done");
},
function(err) {
if (err) {
console.log("error");
} else {
console.log("completely done");
}
}
);
输出:
current: 1
error
current: 2
current: 3
current: 4
current: 5
current: 6
undefined
为什么会有这种奇怪的行为?错误和未定义来自哪里?其他 4 个元素在哪里处理?什么时候调用异步的回调?正如我所料:
current: 1
done
current: 2
done
current: 3
done
current: 4
done
current: 5
done
current: 6
done
current: 7
done
current: 8
done
current: 9
done
current: 10
done
compeletely done
这样只有 6 个元素同时处于活动状态。
我应该改变什么来获得我预期的异步行为?
更新:
如果我使用
async.**eachSeries**([1,2,3,4,5,6,7,8,9,10], function(a, b) {
console.log("current: " + a);
b("done");
},
function(err) {
if (err) {
console.log("error");
} else {
console.log("completely done");
}
}
);
那么输出也很奇怪:
current: 1
error
undefined