gpt4 book ai didi

javascript - 被拒绝的 promise 仍然调用下一个 .then()

转载 作者:太空宇宙 更新时间:2023-11-04 02:52:36 24 4
gpt4 key购买 nike

这是一个 100% 拒绝的 promise 链。我希望打印第一个 console.log ,但之后由于被拒绝的 promise ,它应该跳到最后的 .catch

function stringProcessor(string)
{
return new Promise((resolve, reject) => {
console.log(`Calling ${string}`)
reject(`Rejected ${string}`)
});
}

exports.concat = functions.https.onRequest((request, response) => {
return stringProcessor("foo")
.then(stringProcessor("bar"))
.then(stringProcessor("hello"))
.then(response.send("no problem!"))
.catch(e =>
{
console.log("CAUGHT : " + e)
response.send("not ok")
})
})

但是输出日志是

info: User function triggered, starting execution
info: Calling foo
info: Calling bar
info: Calling hello
info: CAUGHT : Rejected foo
info: Execution took 15 ms, user function completed successfully
error: (node:67568) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Rejected bar
error: (node:67568) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:67568) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): Rejected hello

不仅所有内容都按顺序调用,而且即使 CAUGHT 行清楚地打印,我也收到关于未捕获 promise 的警告。

最佳答案

您需要在then中传递返回promise的函数:

return stringProcessor("foo")
.then(() => stringProcessor("bar"))
.then(() => stringProcessor("hello"))
// ...

在您的示例中,您传递的不是函数,而是 promise 。

关于javascript - 被拒绝的 promise 仍然调用下一个 .then(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45576004/

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