简体   繁体   English

如何在并行作业限制的情况下同时在生成器上运行函数

[英]How to run a function on a generator concurrently with a limit on parallel jobs

I have a generator, which generates lots of, but not infinitely many values.我有一个生成器,它生成很多但不是无限多的值。 I want to call a high-latency function on each of them with n of them concurrently.我想同时对其中的n 个调用一个高延迟函数。

I tried to use async.eachLimit like this.我试图像这样使用async.eachLimit But it prints only 3 numbers, not all of them.但它只打印 3 个数字,而不是全部。

async = require('async');

function* generator(start) {
    for (var i = start; i < start + 100; ++i) {
        yield i;
    }
}

async.eachLimit(generator(17), 3, function(value) {
    console.log(value);
});

What's the right way to achieve this?实现这一目标的正确方法是什么?

I think because you're not passing in an async function it's erroring out on you我认为因为你没有传入一个异步函数,所以你会出错

'use strict';

const async = require('async');

async function* generator(start) {
  for (var i = start; i < start + 100; ++i) {
      yield i;
  }
}

(async () => {
  await async.eachLimit(generator(17), 3, async (value) => {
      console.log(value);
  });
})().catch(e => console.error);

Works for me;为我工作;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM