0

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.

Jeb50
  • 6,272
  • 6
  • 49
  • 87
  • 1
    It returns from the function it is in. If you want to stop execution, you probably shouldn't catch that exception, or at least rethrow it. – tkausl Feb 04 '22 at 06:13
  • @tkausl No `.catch` will result `UnhandledPromiseRejectionWarning` aborting execution, client will never receive any response. – Jeb50 Feb 04 '22 at 16:32

1 Answers1

1

Here we put the call back request of PgTest() Function inside a Promise wrapper Promise( (resolve, reject) => { //callback function}). This wrapper allows you to call the pgTest function like a promise with .catch() methods. When the pgTest is called, it executes the request to the API and waits for either a resolve() or a reject() statement to execute. In the callback function, you simply pass the retrieved data into the resolve or reject methods. check here

The .catch is a wrapper function of a promise and can be used to catch any exception that happens within the .then/inside the block.

enter image description here

So, the return will be performed, and it will went out of the wrapper function. because the .catch wrapper function can handle every exception in the .Catch block

Updated:

enter image description here

Refer here for more information

Delliganesh Sevanesan
  • 4,146
  • 1
  • 5
  • 15