https://github.com/chancehudson/async-express
Allow async/await syntax in Express routes and middleware
https://github.com/chancehudson/async-express
Last synced: over 1 year ago
JSON representation
Allow async/await syntax in Express routes and middleware
- Host: GitHub
- URL: https://github.com/chancehudson/async-express
- Owner: chancehudson
- Created: 2019-03-24T21:48:47.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-06-26T16:35:59.000Z (about 7 years ago)
- Last Synced: 2024-07-21T00:41:09.704Z (about 2 years ago)
- Language: JavaScript
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# async-express
Simple middleware wrapper to make Express handlers async compatible.
## This package is likely unnecessary
You can pass an async function as an express route handler without a problem, just make sure to use `res` and `next` appropriately.
## Usage
`npm install --save async-express`
In a route
```js
const express = require('express');
const asyncExpress = require('async-express');
const app = express();
// As a route
app.get('/', asyncExpress(async (req, res, next) => {
// Wait 5 seconds
await new Promise(r => setTimeout(r, 5000));
// Send a response
res.send('Waited 5 seconds successfully');
}));
```
The sample above can be refactored as the following
```js
const express = require('express');
const asyncExpress = require('async-express');
const app = express();
const waitForABit = asyncExpress(async (req, res, next) => {
// Wait 5 seconds
await new Promise(r => setTimeout(r, 5000));
// Send a response
res.send('Waited 5 seconds successfully');
});
app.get('/', waitForABit);
```