gpt4 book ai didi

javascript - 将数组发送到 promise 链

转载 作者:行者123 更新时间:2023-12-03 09:09:22 24 4
gpt4 key购买 nike

我正在尝试使用相同的函数来检查 promise 链中的值数组。

/*This works*/
dc.checkIfDateIsAfter(departure_date,return_date)
.then(console.log)
.then(dc.checkIfDateIsValid.bind(null,departure_date))
.then(console.log)
.then(rp.bind(null, options))
.then(console.log)
.catch(console.log);

/*This doesn't work*/
dc.checkIfDateIsAfter(departure_date,return_date)
.then(console.log)
.then(Promise.all([dc.checkIfDateIsValid.bind(null,departure_date)])
.then(console.log) // undefined
.then(rp.bind(null, options))
.then(console.log)
.catch(console.log);

如何在无需重写函数来获取数组的情况下完成此操作?

最佳答案

Promise.all 需要一系列 promise 。您在第二次尝试中已经做到了这一点,但是 .then 需要一个返回 promise 的函数。 Promise.all() 评估为一个 promise ,而不是返回一个 promise 的函数,因此您必须像这样重写它:

.then(function () {
return Promise.all([dc.checkIfDateIsValid.bind(null,departure_date)]);
})

关于javascript - 将数组发送到 promise 链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32126750/

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