gpt4 book ai didi

javascript - Bluebird promise 和别名

转载 作者:行者123 更新时间:2023-11-28 18:40:11 27 4
gpt4 key购买 nike

是否有一种方法可以传入 Promise 数组,以便您可以通过给定的别名而不是索引来访问结果?不使用 Props。

像这样的事情:

var a = [{shop: promise(customer)},{customer:promise(customer)}]

Promise.all(a).then(function(res){
console.log(res.shop):
});

Prop 可以用于此目的,但是它不符合我的要求,但以下是如何使用 Prop 来完成此操作,以防有人需要它:

Promise.props({
pictures: getPictures(),
comments: getComments(),
tweets: getTweets()
}).then(function(result) {
console.log(result.tweets, result.pictures, result.comments);
});

最佳答案

Promise.all()只适用于数组,但适用于 ES6:

const a = [promise(customer), promise(customer)];
Promise.all(a)
.then(([shop, customer]) => console.log(shop))

或者,也可以使用 Bluebird 的 Promise.prototype.spread()

var a = [promise(customer), promise(customer)];
Promise.all(a)
.spread(function(shop, customer) {
console.log(shop);
});

关于javascript - Bluebird promise 和别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36240186/

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