# unload

> Execute code when the js-process exits. On all javascript-environments

Latest version **2.4.1** (published 2022-12-28) · Apache-2.0 license · 0 weekly downloads

## Install

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

## Health

**Score 45/100 (D)** — status: abandoned.

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

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.4.1 |
| Published | 2022-12-28 |
| First published | 2016-12-08 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 36.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 58 |
| Author | pubkey |
| Maintainers | pubkey |
| Keywords | unload, exit, onunload, SIGINT, uncaughtException, beforeExit |

## Links

- npm: https://www.npmjs.com/package/unload
- Repository: https://github.com/pubkey/unload
- Homepage: https://github.com/pubkey/unload#readme
- Issues: https://github.com/pubkey/unload/issues
- Funding: https://github.com/sponsors/pubkey
- npm.io page: https://npm.io/package/unload

## Recent versions

- 2.4.1 (latest) — 2022-12-28
- 2.4.0 — 2022-12-28
- 2.3.1 — 2021-11-04
- 2.3.0 — 2021-11-04
- 2.2.0 — 2019-09-26
- 2.1.1 — 2019-07-23
- 2.1.0 — 2018-11-09
- 2.0.2 — 2018-09-20
- 2.0.1 — 2018-07-31
- 2.0.0 — 2018-07-30
- 1.3.9 — 2018-07-04
- 1.3.8 — 2018-07-04
- 1.3.7 — 2018-06-28
- 1.3.6 — 2017-11-18
- 1.3.5 — 2016-12-15
- … 2 more at https://npm.io/package/unload/versions

## README

# javascript: unload

Run a piece of code whenever the javascript process stops/exits/quits. On **browsers, nodejs, electron, react-native, workers, iframes**. It also ensures that the exit-function is called only **once**.

You should use this module when your write a npm library where you dont know in which environments the users will run it.

# What does `unload` handle?

When nodejs:
```js
process.on('beforeExit');
process.on('exit');
process.on('SIGINT'); // catches ctrl+c event
process.on('uncaughtException'); // catches uncaught exceptions
```

When browser:
```js
window.addEventListener('beforeunload'); // closing of normal browser-window
window.addEventListener('unload'); // closed inside of iframe
```

# Usage

Installation:

`npm install unload --save`

Add a function which runs when the process exits:

```javascript
var unload = require('unload');
unload.add(function(){
    console.log('Ouch, I\'m dying.');
});
```

Add and remove the function (It will no longer run when the process exits):

```javascript
var unload = require('unload');
var ret = unload.add(function(){
    console.log('Ouch, I\'m dying.');
});

ret.remove(); // removes the event-handler
```

Run all previously added functions:

```javascript
var unload = require('unload');
unload.add(function(){
    console.log('Ouch, I\'m dying.');
});

unload.runAll();
```

Remove all added functions (They will no longer run when the process exits):
```javascript
var unload = require('unload');
unload.add(function(){
    console.log('Ouch, I\'m dying.');
});

unload.removeAll();
```


# Sponsored by

<p align="center">
    <a href="https://rxdb.info/?utm_source=github&utm_medium=repo&utm_campaign=github-unload">
        <img
            src="https://github.com/pubkey/rxdb/raw/master/docs-src/files/logo/logo_text.svg"
            alt="Sponsored by RxDB"
            width="300"
         />
         <br />
         <br />
         <span>The <b>JavaScript Database</b></span>
    </a>
</p>

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