在RSVP.js中,有一个非常优雅的成语:
var promises = [2, 3, 5, 7, 11, 13].map(function(id){
return getJSON("/post/" + id + ".json");
});
RSVP.all(promises).then(function(posts) {
// posts contains an array of results for the given promises
}).catch(function(reason){
// if any of the promises fails.
});但是,我使用的是一个已经依赖的库,并公开了一些蓝鸟api。因此,我宁愿避免混入RSVP.js,即使它有时看起来更优雅。
在蓝鸟中,与上面的RSVP.js代码片段相比,什么是等价的?
发布于 2014-08-18 15:57:56
除了使用蓝知更鸟的Promise命名空间而不是RSVP之外,一切都可以保持不变--使用Promise.all。此外,符合承诺A+规范的混合承诺应该运行良好,因此您甚至不必更改任何内容。
虽然我个人不太喜欢它,但是蓝知更鸟也有自己的成语-- Promise.map。
Promise.map([2, 3, 5, 7, 11, 13], function(id){
return getJSON("/post/" + id + ".json");
}).then(function(posts) {
// posts contains an array of results for the given promises
}).catch(function(reason){
// if any of the promises fails.
});https://stackoverflow.com/questions/25367146
复制相似问题