Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

async-exit-hook

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-exit-hook

Run some code when the process exits (supports async hooks and pm2 clustering)

  • 2.0.1
  • unpublished
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.5M
decreased by-16.13%
Maintainers
1
Weekly downloads
 
Created

What is async-exit-hook?

The async-exit-hook npm package allows you to register asynchronous functions that will be called when the Node.js process is exiting. This is useful for cleanup tasks, such as closing database connections, saving state, or other asynchronous operations that need to be completed before the process exits.

What are async-exit-hook's main functionalities?

Registering an asynchronous exit hook

This feature allows you to register an asynchronous function that will be called when the process is exiting. The function can perform any necessary cleanup tasks and must call the provided callback when done.

const exitHook = require('async-exit-hook');

exitHook(async (callback) => {
  console.log('Process is exiting...');
  await new Promise(resolve => setTimeout(resolve, 1000));
  console.log('Cleanup done.');
  callback();
});

// Simulate process exit
die();

Registering multiple exit hooks

You can register multiple asynchronous exit hooks. Each hook will be called in the order they were registered, allowing for complex cleanup sequences.

const exitHook = require('async-exit-hook');

exitHook(async (callback) => {
  console.log('First hook');
  await new Promise(resolve => setTimeout(resolve, 500));
  callback();
});

exitHook(async (callback) => {
  console.log('Second hook');
  await new Promise(resolve => setTimeout(resolve, 500));
  callback();
});

// Simulate process exit
die();

Handling signals

The package can handle various signals (e.g., SIGINT, SIGTERM) and execute the registered hooks when such signals are received, ensuring proper cleanup before the process exits.

const exitHook = require('async-exit-hook');

exitHook(async (callback) => {
  console.log('Received signal, cleaning up...');
  await new Promise(resolve => setTimeout(resolve, 1000));
  console.log('Cleanup done.');
  callback();
});

// Simulate receiving a signal
process.kill(process.pid, 'SIGINT');

Other packages similar to async-exit-hook

Keywords

FAQs

Package last updated on 03 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc