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

标签 javascript node.js google-cloud-functions

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

相关文章:

javascript - 如何在 extjs 小部件列中获取 rowIndex

javascript - angularjs 添加值无法设置未定义的属性 'id'

node.js - Mongoose 中间件预更新

node.js - 在 Firebase 函数和 React 之间共享代码

javascript - 无法读取未定义的属性 'user_id'

node.js - Firebase 云函数错误 : connect ECONNREFUSED

javascript - 多维数组javascript中的搜索功能问题

javascript - 使用距离查找纬度和经度

javascript - postman 为 node.js 端点返回 404

android - 如何从 Android 应用程序将倒数计时器与服务器同步