我正在使用npm异步库(https://caolan.github.io/async/docs.html)来限制并行请求的数量。 以下是我的代码:
async.eachLimit(listOfItemIds, 10, function(itemId)
{
console.log("in with item Id: ",itemId);
}, function(err) {
if(err)
{
console.log("err : ",err);
throw err;
}
});
但是它不会对所有listOfItemIds执行,它只对前10个执行并退出。
以下是输出:
in with item id: 252511893899
in with item id: 142558907839
in with item id: 273235013353
in with item id: 112966379563
in with item id: 192525382704
in with item id: 253336093614
in with item id: 112313616389
in with item id: 162256230991
in with item id: 282981461384
in with item id: 263607905569
答案 0 :(得分:1)
您还需要传递一个callback()方法。
这里看看下面的代码,这将有效。
ggplot() +
geom_boxplot(data = subset(so.data, live == 't'),
aes(x = id, y = result, group = resource), color = 'green') +
geom_point(data = subset(so.data, live == 'f'),
aes(x = id, y = result), color = 'red', size = 3) +
scale_x_continuous(breaks = c(101:109), minor_breaks = NULL) +
geom_vline(xintercept = 104.15, linetype = 'dashed')
这将打印数组中的所有元素,在上述情况下,我将并行执行的数量限制为async.eachLimit(listOfItemIds, 2, function(itemId, callback)
{
console.log("in with item Id: ",itemId);
callback();
}, function(err) {
if(err)
{
console.log("err : ",err);
throw err;
}
});
。
希望这有帮助!