这是一个 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/