pg-promise icon indicating copy to clipboard operation
pg-promise copied to clipboard

Azure Function: returns to client request directly

Open TLKG opened this issue 3 years ago • 1 comments
trafficstars

I've noticed this behavior if Azure Function uses async

  • pg-promise immediately returns a successful status regardless execution result to caller before execution continues to a callback or return Promise.resolve/reject.
  • this is regardless execution result.

Here is a test using VSC:

Step 1, create an Azure Function as httptrigger, anonymous Step 2, paste this code into index.js

module.exports = async function(context)  // async is the key, without it works great. I put in async as await is needed in my project.
{
    try
    {
        db.pgTest({}).then
        (
            ok =>
            {
                context.res = {status: 222, body: ok};
                context.done();
            },
            err =>
            {
                context.res = {status: 555, body: "this is error"};
                context.done();
            }
        );
    }
}

 const pgTest = (param) =>
{
    var sql = 'select * from current.goodfunc()';  // or non_existfunc()
     db.any(sql)
        .then
        (
            good => 
            { 
                return Promise.resolve(good);  // set breakpoint here to observe if client like Postman gets any response after this
            },
            bad => 
            { 
                return Promise.reject(bad);   // set breakpoint here to observe if client like Postman gets any response after this
            }
        );
}

TLKG avatar Jan 14 '22 18:01 TLKG

You need to await, or return your promises:

await db.pgTest({}).then (/* ... */)

return db.any(sql).then (/* ... */)

boromisp avatar Jan 15 '22 08:01 boromisp

I am not familiar with Azure, but I presume that what @boromisp wrote above, must be some kind of limitation of your environment.

vitaly-t avatar Aug 27 '22 22:08 vitaly-t