Skip to content

Catch Node.js exit conditions, including errors and unhandled rejections, and fire callbacks.

License

CC0-1.0, MIT licenses found

Licenses found

CC0-1.0
LICENSE-CC0
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

electrovir/catch-exit

Repository files navigation

catch-exit

Catch Node.js exit conditions, including errors and unhandled promise rejections.

  • Warns you of async exit callbacks (which won't complete as expected).
  • Returns proper error codes for the given signal or error source (for example 130 for the SIGINT signal or 7 for errors originating from your provided exit handlers).
  • Allows you to add and remove process exit, death, or error listeners.
  • Turns unhandled promise rejection warnings into errors.

Note that v2 and onwards of this package are ESM only.

Install

npm install catch-exit

Usage

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();
    }
});

Async Warning

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();
    }
});

About

Catch Node.js exit conditions, including errors and unhandled rejections, and fire callbacks.

Resources

License

CC0-1.0, MIT licenses found

Licenses found

CC0-1.0
LICENSE-CC0
MIT
LICENSE-MIT

Stars

Watchers

Forks