Steps to reproduce:
Step 1. VSC, F1 to create an auto-gen Azure Function (AF).
Step 2. index.js, replace it with below following 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
}
Step 3. create db.pgTest(), a standard Promise. Can be any db calls I'm using 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.
}
)
}
);
}
Environment:
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
Step 4. F5 and hit the URL.
Update
My question of return
is about index.js, block ce => { ... return; }
. I overlooked this block indeed is a function.