The response is buffered on the server, so the code won't wait for the response to actually be sent to the client. A slow connection doesn't make the server code run slower.
Still, your server code will be more efficient if you first fetch all the data that you need from the database, then close the database connection and use the data to create the response. That way you can have the database connection open as short a time as possible, using less database resources.
Side note: I see that you use select *
in your code, which fetches all fields from the table. You should specify only the fields that you actually will use, so that you fetch as little data as possible from the database.