错误:当收到并处理拒绝时,return 不会停止执行

Bug: A return won't stop execution when a reject is received and handled

重现步骤:

第 1 步。VSC,F1 创建自动生成的 Azure 函数 (AF)。

第二步index.js,将其替换为下面的MS AF Js developer guide

module.exports = async function (context) 
{
    const good = await db.pgTest({})  // this call will receive a Promise.reject, see below
        .catch
        (
            ce => 
            {
                context.res = {status: 401, body: ce};
                return;   // execution supposed end, but it goes on
            }
        );
    var x = good[0]["column1"];  
    // execution continues to above line even .catch() has a return
    // 'good' will have an array when db.pgTest() succeeds, now is 'undefined'
    // but strangely terminal doesn't print out exception like:
    // Exception: TypeError: Cannot read property '0' of undefined, Stack: TypeError: Cannot read property '0' of undefined    

}

第 3 步。创建 db.pgTest(),一个标准的 Promise。可以是我正在使用的任何数据库调用 pg-promise:

const pgTest = (param) =>
{
    return new Promise((resolve, reject) =>
    {
        var sql = 'select * from schema.nonexits()';  // simply call a non-exists function to raise a runtime error
        db.any(sql)
        .then
        (
            good => resolve(good),
            bad => 
            {
                reject({status: 401, bad: bad});
            }
        )
        .catch
        (
            ce =>
            {
                reject({status: 402, bad: ce});   // because .then(..., bad=>...) this block is never reached. 
            }
        )
    }
    );
}

环境:

Version: 1.63.1 (system setup)
Commit: fe719cd3e5825bf14e14182fddeb88ee8daf044f
Date: 2021-12-14T02:13:54.292Z
Electron: 13.5.2
Chromium: 91.0.4472.164
Node.js: 14.16.0
V8: 9.1.269.39-electron.0
OS: Windows_NT x64 10.0.19044
Azure Functions Extension: v1.6.0

步骤 4. F5 并点击 URL。

更新 我的return问题是关于index.js,块ce => { ... return; }。我忽略了这个块确实是一个 function.

这里我们将 PgTest() 函数的回调请求放在 Promise 包装器 Promise( (resolve, reject) => { //callback function}) 中。这个包装器允许您像使用 .catch() 方法一样调用 pgTest 函数。当 pgTest 被调用时,它执行对 API 和 waits 的请求要执行的 resolve()reject() 语句。在回调函数中,您只需将检索到的数据传递给 resolve 或 reject 方法。检查 here

.catch 是 promise 的包装函数,可用于捕获 .then/inside the 块内发生的任何异常。

所以,return会被执行,它会跳出包装函数。因为 .catch 包装函数可以处理 .Catch

中的每个异常

已更新:

参考了解更多信息