gpt4 book ai didi

javascript - 想要基于一系列其他 promise completioen 返回一个 promise

转载 作者:行者123 更新时间:2023-11-29 16:47:17 24 4
gpt4 key购买 nike

这是我的代码:

var delete_card = function(){

stripe.customers.deleteCard(
this.service_id.stripe,
this.credit_card[0].stripe_id
)
.then(function(obj){
this.recipients.splice(0, 1);
return this.save()
})
}

stripe 调用和 save 调用都返回 promise 。

我如何从 delete_card 方法中返回一个 promise ?

我会像新的 Promise 一样将它们全部包装起来并从那里返回一个 promise 吗?
我将如何构建它,以便我冒出错误和结果?

我希望能够像这样从调用者那里做一些事情:

delete_card
.then(...)
.catch(...)

然后继续编写链式 promise?

最佳答案

只需在 delete_card 函数中返回 promise。

var delete_card = function(){

/************/
/**/return/**/ stripe.customers.deleteCard(
/************/
this.service_id.stripe,
this.credit_card[0].stripe_id
)
.then(function(obj){
this.recipients.splice(0, 1);
return this.save()
})
}

编辑:我把添加return 的地方变得非常明显,因为代码差异是如此微妙。

关于javascript - 想要基于一系列其他 promise completioen 返回一个 promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39967235/

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