【问题标题】:javascript - bluebird is not starting promises correctlyjavascript - 蓝鸟没有正确启动承诺
【发布时间】:2016-12-14 09:45:37
【问题描述】:

我遇到了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 正在同时开始我的所有承诺。 你能告诉我我的代码有什么问题吗?谢谢

【问题讨论】:

  • 你打错了)你将concurrency设置为Array.map而不是Promise.all。最好在承诺链的每一步都返回一些东西

标签: promise bluebird


【解决方案1】:

你使用的是 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);
 });

【讨论】:

    猜你喜欢
    • 2014-11-23
    • 2016-08-26
    • 1970-01-01
    • 2015-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    • 2014-11-06
    相关资源
    最近更新 更多