Catch Node.js exit conditions, including errors and unhandled promise rejections.
130
for the SIGINT
signal or 7
for errors originating from your provided exit handlers).Note that v2 and onwards of this package are ESM only.
npm install catch-exit
This package is primarily used through its addExitCallback
export:
import {addExitCallback} from 'catch-exit';
function mySyncCleanup() {}
async function myAsyncCleanup() {}
addExitCallback(mySyncCleanup);
/** Multiple callbacks can be registered. */
addExitCallback((signal) => {
/** See the "Async Warning" part of the README for an explanation on async weirdness. */
if (signal !== 'exit') {
myAsyncCleanup();
}
});
You may see the following logged to your terminal:
Warning: an async 'process.exit' callback was used; it will not run to completion as 'process.exit' will not complete async tasks.
This means that you're creating async tasks from within an exit callback that is used in process.exit
. process.exit
does not allow async tasks to complete so your callbacks will not complete, contrary to what you might expect. To prevent this warning, wrap your async cleanup in an if statement that excludes 'exit'
signals from doing anything async:
import {addExitCallback} from 'catch-exit';
async function myAsyncCleanup() {}
addExitCallback((signal) => {
if (signal !== 'exit') {
myAsyncCleanup();
}
});