I want to execute an asynchronous callback when exit event is emitted. For example:
process.addListener ("exit", function (){
asynchronousCode (function (){
//my callback
});
});
I’ve tried both with addListener and on but the code inside the callback is never executed because the program emits the exit event, executes the asynchronous function and terminates without calling the callback.
How can I force the program to wait until I execute process.emit ("exit")? Or maybe the code must be synchronous…
From the documentation:
Emitted when the process is about to exit. This is a good hook to perform constant time checks of the module’s state (like for unit tests). The main event loop will no longer be run after the ‘exit’ callback finishes, so timers may not be scheduled.
So you cannot use an asynchronous function in this handler.