gpt4 book ai didi

javascript - 将 RSVP.js map idiom 移植到 bluebird

转载 作者:行者123 更新时间:2023-11-30 12:38:16 24 4
gpt4 key购买 nike

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.
});

但是我使用的库已经依赖并公开了一些 bluebird's api .所以我宁愿避免混入 RSVP.js,即使它有时看起来更优雅。

bluebird 中的等价物是什么, 上面的 RSVP.js 代码片段?

最佳答案

除了使用 Bluebird 的 Promise 命名空间而不是 RSVP 之外,一切都可以保持不变 - 使用 Promise.all .此外,混合符合 Promises A+ specification 的 promise 应该运行良好,因此您甚至可能无需更改任何内容。

虽然我个人不太喜欢它,但 Bluebird 对此任务也有自己的成语 - 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.
});

关于javascript - 将 RSVP.js map idiom 移植到 bluebird,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25367146/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com