Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pacifiquem/node-async-handler
Async Error Handling Middleware for ExpressJS with ESM/TypeScript support.
https://github.com/pacifiquem/node-async-handler
async error error-handling express handling node nodejs try-catch
Last synced: about 2 months ago
JSON representation
Async Error Handling Middleware for ExpressJS with ESM/TypeScript support.
- Host: GitHub
- URL: https://github.com/pacifiquem/node-async-handler
- Owner: pacifiquem
- License: mit
- Created: 2023-03-25T07:38:36.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-01-09T17:28:57.000Z (12 months ago)
- Last Synced: 2024-11-02T07:33:32.703Z (about 2 months ago)
- Topics: async, error, error-handling, express, handling, node, nodejs, try-catch
- Language: TypeScript
- Homepage:
- Size: 208 KB
- Stars: 26
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# 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:
```ts
// 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:
>
> ```ts
> import pkg from 'node-async-handler';
> const { asyncHandler } = pkg;
>```> **Note** : To import ```node-async-handler``` in CommonJS:
>
> ```ts
> const { asyncHandler } = require('node-async-handler');
>```## License
This project is licensed under the MIT License. See the [LICENSE file](https://github.com/pacifiquem/node-async-handler/blob/main/LICENSE) for details.
Author: MURANGWA Pacifique