I have a serverless framework node js function that initializes mysql DB and then runs a query, ES6 is used to return an HTTP response at the end
Is there a way to return the response before waiting for the DB call to finish, for example in node js - res.send will return immediately and the function continues to run after the response is returned
Is there a better way to initialize a new mysql pool (perhaps reuse the pool instead of creating an ending it in each request?)
a: If for example the answer is to create the pool outside the function, how does it exactly work and how does the lambda function reuse it between requests, when does it get destroyed and how does it know when to recreate the pool
import mysql from "mysql";
export const myFunc = async (event) => {
try {
const pool = mysql.createPool({...}); // Creates a new mysql pool
await new Promise((resolve, reject) => {pool.query(
"INSERT STATEMENT", [...params], (error, results) => {
if(error) reject(error)
resolve(results)
}
)});
pool.end();
return {
statusCode: 200,
body: JSON.stringify({message: 'End'}),
};
} catch (err) {
return {
statusCode: 500,
body: JSON.stringify({message: 'Error'}),
};
}
};