2016-12-13 34 views
1

我遇到bluebirdconcurreny問題。基本上我希望我的承諾能夠一個接一個地被解僱。我發現這可以使用bluebird完成。這是我的代碼:javascript - 藍鳥未正確啓動承諾

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("[email protected]"); 
}, {concurrency: 1})) 
.then(function(all){ 
    console.log(all); 
}) 
.catch(function(err){ 
    console.log(err); 
}); 

我所尊重的是類似於({concurrency: 1})。

[email protected] ...start ... 
loading: [email protected]@latest 
loading: [email protected]@latest 
loading: [email protected]@latest 
loading: [email protected]@latest 
.... 
[email protected] ...start ... 
loading: [email protected]@latest 
loading: [email protected]@latest 
loading: [email protected]@latest 
loading: [email protected]@latest 

... 等

,但我有什麼:

[email protected] ...start ... 
[email protected] ...start ... 
[email protected] ...start ... 
[email protected] ...start ... 
loading: [email protected]@latest 

這意味着bluebird是我的開始在同一時間所有的承諾。 你能告訴我我的代碼有什麼問題嗎?感謝

+0

你有一個錯字)你把'concurrency'設置爲'Array.map'而不是'Promise.all'。最好還是在承諾鏈的每一步都要回報一些東西 – stasovlas

回答

2

您正在使用陣列#地圖不Promise.map

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

Array.map並不瞭解{併發:1}的說法 - 它使用的thisArg回調

使用Promise.map,像這樣使用Promise.map

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