Skip to content

Instantly share code, notes, and snippets.

@darkxanter
Created January 8, 2020 08:07
Show Gist options
  • Select an option

  • Save darkxanter/30222456d9d34629ea063bcff95fca69 to your computer and use it in GitHub Desktop.

Select an option

Save darkxanter/30222456d9d34629ea063bcff95fca69 to your computer and use it in GitHub Desktop.
Node js block script exit
process.stdin.resume(); //so the program will not close instantly
const exitHandler = (options: { cleanup?: boolean; exit?: boolean }) => (exitCode?: any) => {
if (options.cleanup) console.log('clean');
if (exitCode || exitCode === 0) console.log(exitCode);
if (options.exit) process.exit();
};
//do something when app is closing
process.on('exit', exitHandler({ cleanup: true }));
['SIGINT', 'SIGTERM', 'SIGUSR1', 'SIGUSR2', 'uncaughtException']
.forEach((it: any) => process.on(it, exitHandler({ exit: true })));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment