is it ok to stop execution of async route handler in an express app?

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.

  1. 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 errors is truthy, then you never send any response.

  2. There does not appear to be any reason for you to declare this route handler async as there is no use of await.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top