npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

crisphooks

v3.0.0

Published

Simple asynchronous/synchronous hooks for Node.JS and browsers

Downloads

734

Readme

crisphooks

Simple and powerful asynchronous/synchronous hooks for Node.JS and browsers.

Installing

npm install crisphooks

Asynchronous or Synchronous Hooks

Hooks registered using the hook() method can either return scalar values or Promises. If it returns a Promise, the hook finishes executing before the next hook starts.

import { CrispHooks } from 'crisphooks';
const hooks = new CrispHooks();

hooks.hook('sleep', function(amount) {
	console.log('Sleeping for ' + amount);
});

hooks.hook('sleep', async function(amount) {
	await new Promise((resolve) => setTimeout(resolve, amount));
});

hooks.hook('sleep', function() {
	console.log('zzzz...');
});

hooks.trigger('sleep', 5000).then(function() {
	console.log('Done sleeping.');
});

Output:

Sleeping for 5000
zzzz...
Done sleeping.

Can execute synchronously with no async hooks

If there are no asynchronous hooks, hooks can be executed synchronously. If .triggerSync() is used when there are registered asynchronous hooks, all asynchronous hooks are executed in parallel and errors from them result in a global exception being thrown.

hooks.hook('doSomething', function() {
	console.log('Something.');
});

hooks.triggerSync('doSomething');

Hook Priorities for Execution Order

Priorities can be specified as a fourth argument to .hook() Lower priorities execute first. If no priority is specified, it defaults to 0. If multiple hooks are registered with the same priority, they are executed in the order they are registered.

hooks.hook('doSomething', function() {
	console.log('Do something second');
}, null, 5);

hooks.hook('doSomething', function() {
	console.log('Do something first');
}, null, 2);

Error Handling

When registering a hook, you can also register an error cleanup function with it. This function should undo any effects of the hook that need to be undone on error. If an error occurs in the chain of hooks, the error cleanup function of any previous hooks (not including the hook that generated the error) are called in reverse order.

hooks.hook('doSomething', function() {
	console.log('Doing something 1.');
}, function(error) {
	console.log('Handling error 1', error);
});

hooks.hook('doSomething', function() {
	console.log('Doing something 2.');
	throw new Error('Some Error');
});

hooks.trigger('doSomething').catch(function(error) {
	console.log('Got error', error);
});

Output:

Doing something 1.
Doing something 2.
Handling error 1 Some Error
Got error Some Error

Error handler hooks can be manually called using the triggerError() or triggerErrorSync() methods. The error handlers are called in the reverse order of hook priority.

Mongoose-style Hooks/Middleware

CrispHooks also supports mongoose-style pre/post hooks.

import { CrispPrePostHooks } from 'crisphooks';
const hooks = new CrispPrePostHooks();

// Pre hooks are asynchronous.  They are registered as async hooks with the name: pre-someEvent
hooks.pre('someEvent', function() {
	console.log('Pre someEvent');
});

// Post hooks are synchronous.  They are registered as sync hooks with the name: post-someEvent
hooks.post('someEvent', function() {
	console.log('Post someEvent');
});

hooks.triggerPre('someEvent').then(function() {
	console.log('Done with pre.');
	hooks.triggerPost('someEvent');
	console.log('Done with post.');
});

Output:

Pre someEvent
Done with pre.
Post someEvent
Done with post.

These can be combined and interspersed with normal hooks. CrispPrePostHooks inherits from CrispHooks. Registering a pre hook is equivalent to registering a hook with its name prefixed with pre-. Similarly, a post hook is just the hook name prepended by post-.

Mixins

A few mixins are provided to add hooks to existing classes.

import { CrispHooksMixin, CrispPrePostHooksMixin } from 'crisphooks';
const MyClassWithHooks = CrispHooksMixin(MyClass);
const MyClassWithPPHooks = CrispPrePostHooksMixin(MyClass);

A mixin is also provided to add EventEmitter-style methods on top of a CrispHooks class:

import { EventEmitterMixin } from 'crisphooks';
const MyEventEmitterHooksClass = EventEmitterMixin(MyCrispHooksClass);