# prexit

> A graceful way to shutdown / handle process exit

Latest version **2.3.0** (published 2024-07-31) · WTFPL license · 0 weekly downloads

## Install

```sh
npm install prexit
pnpm add prexit
yarn add prexit
bun add prexit
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.3.0 |
| Published | 2024-07-31 |
| First published | 2019-08-26 |
| Weekly downloads | 0 |
| License | WTFPL |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=12 |
| Dependencies | 0 |
| Unpacked size | 6.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 132 |
| Author | Rasmus Porsager |
| Maintainers | porsager |

## Links

- npm: https://www.npmjs.com/package/prexit
- Repository: https://github.com/porsager/prexit
- Homepage: https://github.com/porsager/prexit#readme
- Issues: https://github.com/porsager/prexit/issues
- npm.io page: https://npm.io/package/prexit

## Recent versions

- 2.3.0 (latest) — 2024-07-31
- 2.2.0 — 2023-03-30
- 2.1.0 — 2023-03-30
- 2.0.2 — 2023-01-10
- 2.0.1 — 2023-01-01
- 2.0.0 — 2022-12-22
- 1.0.0 — 2021-11-26
- 0.0.5 — 2019-12-23
- 0.0.4 — 2019-12-18
- 0.0.3 — 2019-12-17
- 0.0.2 — 2019-08-28
- 0.0.1 — 2019-08-26

## README

[![version](https://img.shields.io/npm/v/prexit.svg)]() [![license](https://img.shields.io/github/license/porsager/prexit.svg)]()

# 🚪 Prexit

A graceful way to shutdown / handle **PR**ocess **EXIT** - way better than other \*rexits

```js
import prexit from 'prexit'

prexit((signal, [err]) => {
  // Do cleanup before shutdown
  // Return a promise to delay exit
  // set prexit.code to exit with non 0
  // eg prexit.code = 1
})
```

Here's a sample for shutting down an http server and database.
First we stop the http server from accepting any new connections.
Then we gracefully close the database connection to allow any pending queries to resolve.

```js
prexit(async () => {
  await new Promise(r => server.close(r))
  await db.end({ timeout: 5 })
})
```

Prexit is a simple function that takes a callback. This will be called with the signal and exit code / error on the following `process` events.

`exit | beforeExit | uncaughtException | unhandledRejection | SIGTSTP | SIGQUIT | SIGHUP | SIGTERM | SIGINT`

You can call prexit as many times as you'd like so you can do cleanup in the relevant places in your code. Prexit will await all promises that callbacks returns, and will ensure they are only called once. After all the promises are settled prexit will call `prexit.ondone()` which defaults to calling `process.exit(prexit.code)`.

If you need to do synchronous cleanup after any async cleanup and right before prexit.ondone is called, you can use `prexit.last(fn)`

```js
prexit.last(() => {
  // This will run after any async handlers right 
  // before exit, meaning only sync cleanup
  // eg. (kill child processes)
})
```

You can also supply optional events to listen for as the first argument of prexit.
 
```js
prexit('uncaughtException', (signal, err) => /* uncaughtException */ )
prexit(['SIGHUP', 'SIGTERM'], (signal, err) => /* SIGHUP | SIGTERM */ )
````

---
_Source: https://npm.io/package/prexit · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
