Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/loulafripouille/asyncable
Wrap async/await middleware function for Express. Catches error and passes it to next.
https://github.com/loulafripouille/asyncable
async-await express middleware nodejs
Last synced: about 1 month ago
JSON representation
Wrap async/await middleware function for Express. Catches error and passes it to next.
- Host: GitHub
- URL: https://github.com/loulafripouille/asyncable
- Owner: loulafripouille
- License: mit
- Created: 2018-03-21T14:51:05.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T01:52:51.000Z (almost 2 years ago)
- Last Synced: 2024-08-09T08:09:05.326Z (5 months ago)
- Topics: async-await, express, middleware, nodejs
- Language: JavaScript
- Size: 773 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# express-asyncable
Wraps async/await Express middlewares. Catches error and passes it to next.
Avoid wrinting try/catch in your middlewares.[![Build Status](https://travis-ci.org/laudeon/asyncable.svg?branch=master)](https://travis-ci.org/laudeon/asyncable)
[![npm version](https://badge.fury.io/js/express-asyncable.svg)](https://badge.fury.io/js/express-asyncable)## Why?
DRY when catching errors from asyn/await Express middlewares.
This module exposes a wrapper for param middleware as well, which is using a different signature `(req, res, next, value)`.## API
### .middleware(function)
Where `function` returns a promise and accepts `req`, `res`, `next` arguments.
### .paramMiddleware(function)
Where `function` returns a promise and accepts `req`, `res`, `next` and `value` arguments.
## Usage example
```js
import { middleware, paramMiddleware } from "asyncable"
// const { middleware, paramMiddleware } = require('asyncable')
// ...router.param('userId', paramMiddleware(model.findObject.bind(model)))
router.route('/')
.get(
// ACL middleware or whatever before for instance...
middleware(userController.list.bind(userController))
)
.post(
// ACL middleware or whatever before for instance...
middleware(userController.add.bind(userController))
)
```