https://github.com/blackglory/you-died
🌿 Decently exit your Node.js application when it is interrupted.
https://github.com/blackglory/you-died
library nodejs npm-package typescript
Last synced: 3 months ago
JSON representation
🌿 Decently exit your Node.js application when it is interrupted.
- Host: GitHub
- URL: https://github.com/blackglory/you-died
- Owner: BlackGlory
- License: mit
- Created: 2021-03-28T13:16:26.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-11-25T17:44:05.000Z (over 2 years ago)
- Last Synced: 2025-08-09T19:40:58.107Z (11 months ago)
- Topics: library, nodejs, npm-package, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/you-died
- Size: 263 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# you-died
Decently exit your Node.js application when it is interrupted.
## Motivation
I need to do an asynchronous cleanup action on exit, none of the modules worked for me:
- exit-hook
- async-exit-hook
- death
- node-cleanup
So I tried it myself, and finally found this:
```ts
process.once('uncaughtException', async () => {
await cleanup()
process.exit(0)
})
process.once('SIGINT', () => { throw new Error() })
```
## Install
```sh
npm install --save you-died
# or
yarn add you-died
```
## Usage
```ts
import youDied from 'you-died'
// or whatever name you feel is decent.
const cancel1 = youDied(async () => {
await cleanup1()
})
const cancel2 = youDied(async () => {
await cleanup2()
})
```
## Under the hood
This module listens to these events:
- `uncaughtException`
- `SIGINT`
- `SIGTERM`
- `SIGBREAK`
Signal event handlers will throw an error named `Signal`,
which is caught by the `uncaughtException` handler.
## API
### youDied
```ts
function youDied(cleanup: () => void | PromiseLike): () => void
```
You can call this function multiple times,
and the `cleanup` function will run sequentially.