Is it ok to stop execution of async route handler with return statement in an Express app?
Yes, it is perfectly fine. Express itself isn’t paying any attention to the promise returned from your
async function so you are under no obligation to resolve that promise with some particular value. When your desired code flow dictates, you can stop further processing of the route handler with
return at any time.
I understand that if I return from an async function, a Promise object will be returned. But, it will be simply garbage collected in this case, right?
Yes, that is correct. The promise will be garbage collected.
A few other observations about this request handler.
If there are no errors, this request handler just leaves the request hanging, never sending a response. All paths through the code should either send a response or forward the request to other code that will send a response. In your case if
errorsis truthy, then you never send any response.
There does not appear to be any reason for you to declare this route handler
asyncas there is no use of
CLICK HERE to find out more related problems solutions.