JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 18517
  • Score
    100M100P100Q147794F
  • License (MIT or CC0 1.0)

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

Package Exports

  • catch-exit
  • catch-exit/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (catch-exit) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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