gpt4 book ai didi

javascript - Firebase Promise 链在 catch block 中终止

转载 作者:行者123 更新时间:2023-12-01 02:36:53 45 4
gpt4 key购买 nike

如果到达第一个 catch block ,我需要退出 http 函数。我正在执行以下操作

    .then() <- start work related with database
.then()
.then()
.catch(error => { <- Here I want to catch errors with firebase database and exit function.
console.log(error)
response.status(500).send(error)
})
.then() <- Here I want to send FCM message if there was no database errors
.catch(error => {
console.log(error)
response.status(200).send("Success") <- Main work with database was finished. I still want to send http 200 and don't care about FCM errors.
})
.then(() => {
response.status(200).send("Success")
}) <-This catch block should be fired if there was an issue with FCM

问题是函数在第一个 catch block 之后继续运行。如何在第一个 catch block 中正确停止该链?谢谢

最佳答案

使用传播值和顶级哨兵的组合,这样的东西应该可以工作:

let bail = false

doWork()
.then(result => {
console.log(result)
return true // indicate success
})
.catch(error => {
console.error(error)
return false // indicate error
})
.then(isPriorSuccessful => {
if (!isPriorSuccessful) {
bail = true
return null
}
else {
// do more stuff here, return a promise
return doMoreWork()
}
})
.catch(error => {
console.error(error)
})
.then(() => {
if (bail) {
res.status(500).send("NOT OK")
return
}
console.log("Just before the end")
res.send("OK")
})

关于javascript - Firebase Promise 链在 catch block 中终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47842492/

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