我如何处理并发以同时只有 2 或 3 个承诺 运行?

How can I handle concurrency to have just 2 or 3 promises running at the same time?

根据bluebird,bluebird 的用法似乎非常简单。 我是这样理解的:

Promise.map(
    Iterable<any>|Promise<Iterable<any>> input,
    function(any item, int index, int length) mapper,
    [Object {concurrency: int=Infinity} options]
) -> Promise

据我所知,我可以有这样的东西:

var promisfunc = Promise.promisify(
  function func(val, cb){
    console.log(val);

    //process data with val ...  
    cb(err, processedData);
  }
);
Promise.map([1, 2, 3, 4, 5, 6, 7, 8, 9],
  function(d){
    return promisfunc(data);
  }, {concurrency: 1} 
).then(function(data){
  console.log(data); // array of data
})
.catch(
  function(err)
  {console.log(err)}
);

我试了好几个小时都没有成功。每次我 运行 所有进程都同时被触发。我如何处理并发以同时只有 2 或 3 个承诺 运行ning?你能解释一下我在这里误解了什么吗?

您对 Promise.map() 在其参数中的期望有根本性的误解。它需要一个标准函数,然后异步地 promisify 到 return。

let Promise = require('bluebird');

var promisfunc =  function func(element, index, length) {
    console.log(element);
    return element;
}

Promise.map([1, 2, 3, 4, 5, 6, 7, 8, 9], promisfunc)
.then( (data) => console.log(data) )
.catch( (err) => console.log(err) );

我做的最重要的事情是摆脱了 var promisfunc = Promise.promisify(function) 因为那不是 Promise.map() 所期望的。它被一个沼泽标准函数所取代,returns 元素在记录它们之后就像一个标准地图函数一样。