deno

Deno: Callback on exit


How do you add a callback for the program exiting?

Implementation in nodejs:

process.on('exit', function (){
    console.log('Goodbye!');
});

Solution

  • You can use unload

    globalThis.addEventListener("unload", () => {
       console.log('goodbye!');
    });
    
    setTimeout(() => console.log('before goodbye'), 1000);
    

    It'll be triggered when calling Deno.exit() as well. You can check more about Program Lifecycle here.