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

before-after-hook

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

before-after-hook

asynchronous before/error/after hooks for internal functionality

  • 1.3.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8.9M
increased by4.44%
Maintainers
1
Weekly downloads
 
Created

What is before-after-hook?

The before-after-hook npm package allows developers to add before, after, and error hooks to JavaScript functions. This is particularly useful for adding custom pre-processing, post-processing, or error handling logic in a clean and organized manner. It can be used in both Node.js and browser environments.

What are before-after-hook's main functionalities?

Creating and using a single hook

This example demonstrates how to create a hook and attach before and after hooks to a function. The before hook logs a message before the main function runs, and the after hook logs a message after the main function completes.

const {Hook} = require('before-after-hook');
const hook = new Hook();

hook.before(addTask, async () => {
  console.log('Before adding task');
});

hook.after(addTask, async () => {
  console.log('After adding task');
});

async function addTask() {
  console.log('Adding task');
}

hook(addTask, []).then(() => {
  console.log('Task added');
});

Error handling with hooks

This example shows how to use the error hook to handle errors that may occur in the main function. The error hook logs the error message, providing a centralized way to handle errors.

const {Hook} = require('before-after-hook');
const hook = new Hook();

hook.error(addTask, async (error) => {
  console.error('Error occurred:', error.message);
});

async function addTask() {
  throw new Error('Failed to add task');
}

hook(addTask, []).catch(err => {
  console.log('Error handling complete.');
});

Wrapping functions with multiple hooks

This example illustrates how to use HookCollection to manage multiple hooks for a single named operation. It demonstrates adding before, after, and error hooks for a 'save' operation, providing a comprehensive example of managing complex asynchronous operations.

const {HookCollection} = require('before-after-hook');
const hooks = new HookCollection();

hooks.before('save', async () => {
  console.log('Before save');
});

hooks.after('save', async () => {
  console.log('After save');
});

hooks.error('save', async (error) => {
  console.error('Error during save:', error.message);
});

async function saveData() {
  console.log('Saving data');
}

hooks('save', saveData, []).then(() => {
  console.log('Data saved');
}).catch(err => {
  console.log('Error handling complete.');
});

Other packages similar to before-after-hook

Keywords

FAQs

Package last updated on 29 Dec 2018

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