I need to send an email when server is shutting down, I'm using nodemailer but email is not sending when I write it before process.exit().
sendMail('Server is shutting down')
process.exit();
I was trying to use "beforeExit" event but it's not working either.
The 'beforeExit' event is not emitted for conditions causing explicit termination, such as calling process.exit() or uncaught exceptions.
As I understand as per the Doc.
Listener functions must only perform synchronous operations. The Node.js process will exit immediately after calling the 'exit' event listeners causing any additional work still queued in the event loop to be abandoned. In the following example, for instance, the timeout will never occur:
process.on('exit', (code) => {
setTimeout(() => {
console.log('This will not run');
}, 0);
});
On exit event it requires a sync call but nodemailer is async.