Reputation: 18427
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...
Upvotes: 0
Views: 285
Reputation: 2702
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.
process.on('exit', function () {
process.nextTick(function () {
console.log('This will not run');
});
console.log('About to exit.');
});
So you cannot use an asynchronous function in this handler.
Upvotes: 3