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 🙏

© 2026 – Pkg Stats / Ryan Hefner

js-async-handler

v1.0.0

Published

A lightweight async handler utility for Express.js middleware that automatically catches errors and passes them to the next middleware

Readme

js-async-handler

A lightweight async handler utility for Express.js middleware that automatically catches errors and passes them to the next middleware in the chain.

Features

  • 🚀 Lightweight and fast
  • 🔧 Works with both ES modules and CommonJS
  • 💪 TypeScript support included
  • 🛡️ Automatic error handling for async functions
  • 📦 Zero dependencies

Installation

npm install js-async-handler

Usage

ES Modules

import { asyncHandler } from 'js-async-handler';
import express from 'express';

const app = express();

// Use with async route handlers
app.get('/users', asyncHandler(async (req, res) => {
  const users = await User.findAll(); // This can throw an error
  res.json(users);
}));

// Error will be automatically caught and passed to error middleware
app.use((err, req, res, next) => {
  console.error(err);
  res.status(500).json({ error: 'Internal Server Error' });
});

CommonJS

const { asyncHandler } = require('js-async-handler');
const express = require('express');

const app = express();

app.get('/users', asyncHandler(async (req, res) => {
  const users = await User.findAll();
  res.json(users);
}));

API

asyncHandler(fn)

Wraps an async function and automatically catches any errors, passing them to the Express error handler.

Parameters

  • fn (Function): An async function that takes (req, res, next) as parameters

Returns

  • (Function): Express middleware function

Example

const getUserById = asyncHandler(async (req, res) => {
  const user = await User.findById(req.params.id);
  if (!user) {
    return res.status(404).json({ error: 'User not found' });
  }
  res.json(user);
});

app.get('/users/:id', getUserById);

Why Use This?

Without asyncHandler, you need to manually catch errors in every async route:

// Without asyncHandler - repetitive try/catch
app.get('/users', async (req, res, next) => {
  try {
    const users = await User.findAll();
    res.json(users);
  } catch (error) {
    next(error);
  }
});

With asyncHandler, errors are automatically handled:

// With asyncHandler - clean and simple
app.get('/users', asyncHandler(async (req, res) => {
  const users = await User.findAll();
  res.json(users);
}));

TypeScript

This package includes TypeScript declarations. No need to install additional @types packages.

import { asyncHandler } from 'js-async-handler';
import { Request, Response } from 'express';

const getUsers = asyncHandler(async (req: Request, res: Response) => {
  const users = await User.findAll();
  res.json(users);
});

License

ISC

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.