【发布时间】:2022-02-11 09:47:17
【问题描述】:
重现步骤:
第 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; }。我忽略了这个块确实是一个函数。
【问题讨论】:
-
它从它所在的函数返回。如果你想停止执行,你可能不应该捕获那个异常,或者至少重新抛出它。
-
@tkausl No
.catch将导致UnhandledPromiseRejectionWarning中止执行,客户端将永远不会收到任何响应。
标签: azure-functions