# graceful-goodbye

> Run cleanup logic just before the process exits without interfering

Latest version **1.3.3** (published 2025-01-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install graceful-goodbye
pnpm add graceful-goodbye
yarn add graceful-goodbye
bun add graceful-goodbye
```

## Health

**Score 25/100 (F)** — status: maintenance-mode.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.3 |
| Published | 2025-01-22 |
| First published | 2021-12-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 5.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 47 |
| Author | Mathias Buus |
| Maintainers | mafintosh |

## Links

- npm: https://www.npmjs.com/package/graceful-goodbye
- Repository: https://github.com/mafintosh/graceful-goodbye
- Issues: https://github.com/mafintosh/graceful-goodbye/issues
- npm.io page: https://npm.io/package/graceful-goodbye

## Dependencies (2)

- [bare-process](https://npm.io/package/bare-process.md) ^4.0.0
- [safety-catch](https://npm.io/package/safety-catch.md) ^1.0.2

## Recent versions

- 1.3.3 (latest) — 2025-01-22
- 1.3.2 — 2024-11-13
- 1.3.1 — 2024-11-12
- 1.3.0 — 2023-06-13
- 1.2.1 — 2023-02-08
- 1.2.0 — 2022-12-23
- 1.1.0 — 2022-08-08
- 1.0.0 — 2021-12-02
- 0.0.0 — 2021-12-02

## README

# graceful-goodbye

Run cleanup logic just before the process exits without interfering with other process handlers.

```
npm install graceful-goodbye
```

## Usage

``` js
import goodbye from 'graceful-goodbye'

goodbye(async function () {
  console.log('i am run before exit')
})
```

## API

#### `const unregister = goodbye(beforeExit, [position = 0])`

Register an async function to be run before process exit.

* Ran when SIGTERM/SIGINT is received
* Ran if the event loop is about to end

If a process signal is received and `graceful-goodbye` is the only signal handler it mimicks the default behaivour of exiting
the process when the exit handlers has run with a 130 exit code. If other handlers are registered, it defers to them to exit the process.

All handlers are deregistered when the beforeExit method runs, which means if the user sends two SIGINTs the second one will always exit the process immediately, assuming no other handlers are registered.

Note that the function is NOT run if the user calls process.exit or if an unhandled error occurs - this is by design.
Those events should exit the process in the same tick as their occur.

#### `goodbye.exit()`

Triggers the cleanup logic (similar effect to receiving a process signal).

#### `goodbye.exiting`

Boolean if the exit code is running.

## Position

``` js
goodbye(async () => console.log('last'), 2)
goodbye(async () => console.log('first'), 0)
goodbye(async () => console.log('middle'), 1)
```

The position value allows you to group handlers, they're executed and awaited by ascending order.

## License

MIT

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