You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Interesting topic,
I was thinking for something like middleware but then before the send
My use-case was that I would like to create a function for cache headers depending of the status code
For that you need access to the response details, like statusCode, current headers etc
Why it works:
The third argument, callback, is a function that you can call in non-async handlers to send a response. The callback function takes two arguments: an Error and a response. When you call it, Lambda waits for the event loop to be empty and then returns the response or error to the invoker.
I too wish that finally could take an async function. I put the MySQL pool (from, ahem, jeremydaly/serverless-mysql) into a request field, and finally gets passed the request object, so it would be the better way to clean up afterward.
For now, instead, I keep the pool in a global variable and assign it to the request but clean up in the handler function:
Activity
MarioVerbelen commentedon Aug 4, 2020
Interesting topic,
I was thinking for something like middleware but then before the send
My use-case was that I would like to create a function for cache headers depending of the status code
For that you need access to the response details, like statusCode, current headers etc
eXist-FraGGer commentedon Aug 4, 2020
I have checked the source code and see why it does not work
The

api.finallysupports async callbackBut the method
res.senddoes not wait for the async method_callbackeXist-FraGGer commentedon Aug 4, 2020
It works if we do the next changes:
2.
eXist-FraGGer commentedon Aug 6, 2020
There is a way how to do some
asyncoperations in finally.callbackfrom lambda functionapi.finally- !!! important - do not useasync / awaitThe third argument,
callback, is a function that you can call in non-async handlers to send a response. The callback function takes two arguments: an Error and a response.When you call it, Lambda waits for the event loop to be empty and then returns the response or error to the invoker.https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html
MarioVerbelen commentedon Aug 6, 2020
api.finally((req, res) ... works great
I see that in the handler you can also use then on run
ammulder commentedon May 6, 2022
I too wish that
finallycould take an async function. I put the MySQL pool (from, ahem, jeremydaly/serverless-mysql) into a request field, andfinallygets passed the request object, so it would be the better way to clean up afterward.For now, instead, I keep the pool in a global variable and assign it to the request but clean up in the handler function: