I have an interesting case that a Promise.reject() not caught in its caller's .catch() but in the try{ }catch{ }.
callDB() calls a pg db function to perform an Insert, when a run-time error (unique-key violation) raised, it goes to .then().catch():
const callDB = (param) =>
{
return new Promise((resolve, reject) =>
{
var sql = "...myInsert($1, $2)";
var dataValue = [param.p1, paramp2];
db.any(sql, dataValue)
.then
(
good => resolve(good),
bad => reject(bad)
)
// Run-Time Err like unique-key violation caught here, returns a reject.
.catch
( dbEr => { return reject(dbEr); })
});
}
f2(), the caller, its callDB().catch() not catching the reject, but the try{} catch{tce}:
async function f2()
{
try
{
...
// callDB's reject NOT caught here.
const good2 = await callDB(param).catch((e2)=>{ return Promise.reject(e2); });
...
}
catch(tce)
{
// callDB's reject caught here!
return Promise.reject(tce);
}
}
An Azure Function calls f2():
module.exports = async function f1(context)
{
try
{
await f2().then
(
good => ...
bad =>
{
// Caught here as Expected!
...
context.res = {status: 401, body: bad};
}
)
.catch
(
(e1) =>
{
context.res = {status: 401, body: "never reach e1"};
}
)
}
catch (etry)
{
context.res = {status: 401, body: "never reach etry"};
}
}
My questions are:
reject goes into f2's try{} catch{}?.then().catch() and try{} catch{}?