被拒绝的承诺仍然调用下一个 .then()

Rejected promise still call to the next .then()

这是一个拒绝 100% 的承诺链。我希望打印第一个 console.log,但在那之后由于被拒绝的承诺,它应该跳到最后的 .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 行,我也收到了关于未捕获承诺的警告。

您需要传递 returns 在 then 中承诺的函数:

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

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