Skip to content

pacifiquem/node-async-handler

Repository files navigation

node-async-handler

Async Error Handling Middleware for Express with ESM Support

This middleware helps you eliminate the need for try-catch blocks throughout your Express application, streamlining error handling and improving code readability.

Note : This is made specifically for ExpressJS projects.

Usage

To use node-async-handler, simply wrap your async functions in the asyncHandler middleware:

// For ESM
import pkg from 'node-async-handler';
const { asyncHandler } = pkg;

// For CommonJS
const { asyncHandler } = require('node-async-handler');

express.get('/', asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
  const result = await someAsyncFunction();
  res.json(result);
}));

In case of an error, the middleware automatically calls next(error), allowing centralized error handling in your Express app.

Importing

Note : To import node-async-handler in ESM:

import pkg from 'node-async-handler';
const { asyncHandler } = pkg;

Note : To import node-async-handler in CommonJS:

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

License

This project is licensed under the MIT License. See the LICENSE file for details.

Author: MURANGWA Pacifique

About

Async Error Handling Middleware for ExpressJS with ESM/TypeScript support.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published