# when-exit

> Execute a function right before the process, or the browser's tab, is about to exit.

Latest version **2.1.5** (published 2025-10-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install when-exit
pnpm add when-exit
yarn add when-exit
bun add when-exit
```

## Health

**Score 60/100 (C)** — status: stable.

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

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.1.5 |
| Published | 2025-10-28 |
| First published | 2022-02-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 15 |
| Maintainers | fabiospampinato |
| Keywords | exit, catch, event, handle, hook, intercept, signal |

## Links

- npm: https://www.npmjs.com/package/when-exit
- Repository: https://github.com/fabiospampinato/when-exit
- Homepage: https://github.com/fabiospampinato/when-exit#readme
- Issues: https://github.com/fabiospampinato/when-exit/issues
- npm.io page: https://npm.io/package/when-exit

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 2.1.5 (latest) — 2025-10-28
- 2.1.4 — 2025-01-12
- 2.1.3 — 2024-06-30
- 2.1.2 — 2023-12-09
- 2.1.1 — 2023-08-03
- 2.1.0 — 2023-02-15
- 2.0.0 — 2022-04-06
- 1.0.0 — 2022-02-08

## README

# WhenExit

Execute a function right before the process, or the browser's tab, is about to exit.

## Install

```sh
npm install when-exit
```

## Usage

```ts
import whenExit from 'when-exit';

// Registering multiple callbacks

onExit ( () => {
  console.log ( 'Callback 1' );
});

onExit ( () => {
  console.log ( 'Callback 2' );
});

// Registering and disposing a callback

const disposer = onExit ( () => {
  console.log ( 'Callback 3' );
});

disposer ();

// Triggering the process to exit

process.exit (); // Callback 1 and 2 are called before exiting
```

## License

MIT © Fabio Spampinato

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