In case my Node.js v.13.12 app faces some technical issue, I'd like:
- to write a log message in the DB;
- to exit from the Node.js app with a specific code.
To achieve such behaviour I use the following code:
const onError = async function onError(error) {
    await logger.log("Critical error: " + error);
    process.exit(UNCAUGHT_FATAL_EXCEPTION);
}
The problem is that when I execute the code above, I reach await logger.log(…), go inside and then immediately continue to process.exit(UNCAUGHT_FATAL_EXCEPTION) and execute it.
As a result, the app is getting closed before the writing to the DB is finished, although I expect from await to wait until the logger.log(…) is done.
How to ensure that process.exit(UNCAUGHT_FATAL_EXCEPTION) will be executed only after logger.log(…) is done without using a callback?
Update:
The entire logger.log(…) code-chain:
export const log = async function log(source, severityNum, severityLevel, limitSeverityNum, functionName, message) {
    if ((severityNum > LOG_LEVELS.OFF.intLevel) && (severityNum <= limitSeverityNum)) {
        await writeLogToDB(source, severityNum, severityLevel, functionName, message);
    }
};
const writeLogToDB = async function writeLogToDB(source, severityNum, severityLevel, functionName, message) {
    try {
        const con = await getConnection();
        con.connect(function (err) {
            if (err) throw err;
            con.query(qryDict.QUERIES.setAddNewLog, [source, severityNum, severityLevel, functionName, message], function (err) {
                try {
                    if (err) {
                        console.error("addNewLog", err);
                    }
                    let response = JSON.stringify({
                        "result": true,
                        "message": "success"
                    });
                } catch (err) {
                    let response = JSON.stringify({
                        "result": false,
                        "message": err
                    });
                    return response;
                } finally {
                    con.close();
                }
            });
        });
    } catch (err) {
        con.close();
        console.error(err);
    }
};
 
    