pg-promise
pg-promise copied to clipboard
Azure Function: returns to client request directly
trafficstars
I've noticed this behavior if Azure Function uses async
pg-promiseimmediately returns a successful status regardless execution result to caller before execution continues to a callback orreturn 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
}
);
}
You need to await, or return your promises:
await db.pgTest({}).then (/* ... */)
return db.any(sql).then (/* ... */)
I am not familiar with Azure, but I presume that what @boromisp wrote above, must be some kind of limitation of your environment.