javascript - bluebird 没有正确启动 promises

javascript - bluebird is not starting promises correctly

我遇到了 bluebird concurreny 的问题。基本上我希望我的承诺一个接一个地被解雇。我发现这可以使用 bluebird 来完成。这是我的代码:

var  getdep = Promise.promisify(
  function getdep(module, cb ) {
    console.log(module + " ...start ...")
    ls(module, function(data) {
      cb(null, data);
    });
  });

 Promise.all([0,1,2,3,].map(function(data){
   return getdep("uglify-js@2.4.24");
 }, {concurrency: 1}))
 .then(function(all){
   console.log(all);
 })
 .catch(function(err){
   console.log(err);
 });

我推崇的是({concurrency: 1}).

uglify-js@2.4.24 ...start ...
loading: uglify-js@2.4.24@latest
loading: uglify-js@2.4.24@latest
loading: uglify-js@2.4.24@latest
loading: uglify-js@2.4.24@latest
....
uglify-js@2.4.24 ...start ...
loading: uglify-js@2.4.24@latest
loading: uglify-js@2.4.24@latest
loading: uglify-js@2.4.24@latest
loading: uglify-js@2.4.24@latest

... 等等

但我所拥有的是:

uglify-js@2.4.24 ...start ...
uglify-js@2.4.24 ...start ...
uglify-js@2.4.24 ...start ...
uglify-js@2.4.24 ...start ...
loading: uglify-js@2.4.24@latest

这意味着 bluebird 同时开始我所有的承诺。 你能告诉我我的代码有什么问题吗?谢谢

您使用的是 Array#map 而不是 Promise.map

 Promise.all(
    [0,1,2,3,].map(function(data){
 //      array.map
         return getdep("uglify-js@2.4.24");
     }, {concurrency: 1}) // end of array.map 
 )
 .then(function(all){
   console.log(all);
 })
 .catch(function(err){
   console.log(err);
 });

Array.map 不理解 {concurrency:1} 参数 - 它使用它作为 thisArg 回调

要使用 Promise.map,请像这样使用 Promise.map

 Promise.map([0,1,2,3,], function(data){
     return getdep("uglify-js@2.4.24");
 }, {concurrency: 1}))
 .then(function(all){
   console.log(all);
 })
 .catch(function(err){
   console.log(err);
 });