Gathering detailed insights and metrics for async-exit-hook-improved
Gathering detailed insights and metrics for async-exit-hook-improved
npm install async-exit-hook-improved
Typescript
Module System
Min. Node Version
Node Version
NPM Version
69.8
Supply Chain
98.7
Quality
75.1
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
644
Last Day
1
Last Week
3
Last Month
44
Last Year
231
83 Commits
1 Watching
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
2.0.3
Package Id
async-exit-hook-improved@2.0.3
Unpacked Size
12.18 kB
Size
4.68 kB
File Count
5
NPM Version
6.14.11
Node Version
14.16.0
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
-66.7%
3
Compared to previous week
Last month
238.5%
44
Compared to previous month
Last year
12.1%
231
Compared to previous year
5
Run some code when the process exits
The process.on('exit')
event doesn't catch all the ways a process can exit. This module catches:
Useful for cleaning up. You can also include async handlers, and add custom events to hook and exit on.
Forked and pretty much rewritten from exit-hook.
$ npm install --save async-exit-hook-improved
process.exit()
and asynchronous codeIf you use asynchronous exit hooks, DO NOT use process.exit()
to exit.
The exit
event DOES NOT support asynchronous code.
['beforeExit' is not emitted for conditions causing explicit termination, such as process.exit()] (https://nodejs.org/api/process.html#process_event_beforeexit)
process.kill(signal)
On windows process.kill(signal)
immediately kills the process, and does not fire signal events,
and as such, cannot be used to gracefully exit. See Clustering and child processes for a
workaround when killing child processes. I'm planning to support gracefully exiting
with async support on windows soon.
If you use custom clustering / child processes, you can gracefully shutdown your child process
by sending a shutdown message (childProc.send('shutdown')
).
1const exitHook = require('async-exit-hook-improved'); 2 3exitHook(() => { 4 console.log('exiting'); 5}); 6 7// you can add multiple hooks, even across files 8exitHook(() => { 9 console.log('exiting 2'); 10}); 11 12// you can add async hooks by accepting a callback 13exitHook(callback => { 14 setTimeout(() => { 15 console.log('exiting 3'); 16 callback(); 17 }, 1000); 18}); 19 20// you can remove a hook 21const hook = () => { 22 console.log('exiting 4'); 23}; 24exitHook(hook); 25exitHook.remove(hook); 26 27// You can hook uncaught errors with uncaughtExceptionHandler(), consequently adding 28// async support to uncaught errors (normally uncaught errors result in a synchronous exit). 29exitHook.uncaughtExceptionHandler(err => { 30 console.error(err); 31}); 32 33// You can hook unhandled rejections with unhandledRejectionHandler() 34exitHook.unhandledRejectionHandler(err => { 35 console.error(err); 36}); 37 38// You can add multiple uncaught error handlers 39// Add the second parameter (callback) to indicate async hooks 40exitHook.uncaughtExceptionHandler((err, callback) => { 41 sendErrorToCloudOrWhatever(err) // Returns promise 42 .then(() => { 43console.log('Sent err to cloud'); 44 }) 45 .catch(sendError => { 46console.error('Error sending to cloud: ', err.stack); 47 }) 48 .then(() => callback); 49}); 50 51// Add exit hooks for a signal or custom message: 52 53// Custom signal 54// Arguments are `signal, exitCode` (SIGBREAK is already handled, this is an example) 55exitHook.hookEvent('SIGBREAK', 21); 56 57// process event: `message` with a filter 58// filter gets all arguments passed to *handler*: `process.on(message, *handler*)` 59// Exits on process event `message` with msg `customShutdownMessage` only 60exitHook.hookEvent('message', 0, msg => msg !== 'customShutdownMessage'); 61 62// All async hooks will work with uncaught errors when you have specified an uncaughtExceptionHandler 63throw new Error('awesome'); 64 65//=> // Sync uncaughtExcpetion hooks called and retun 66//=> '[Error: awesome]' 67//=> // Sync hooks called and retun 68//=> 'exiting' 69//=> 'exiting 2' 70//=> // Async uncaughtException hooks return 71//=> 'Sent error to cloud' 72//=> // Sync uncaughtException hooks return 73//=> 'exiting 3'
MIT © Tapani Moilanen
MIT © Sindre Sorhus
No vulnerabilities found.
No security vulnerabilities found.