README
express-async-await
Write Express middleware and route handlers using async/await
Usage
const { wrapRouter } = require('express-async-await');
const router = wrapRouter(express.Router());
router.get('/foo', async (req, res, next) => {
throw new Error('Exception!');
});
You can just return the body and send back the data to the user.
const { wrapRouter } = require('express-async-await');
const router = wrapRouter(express.Router());
router.get('/foo', async (req) => {
return await new Promise((resolve) => {
resolve({ message: 'Hello!' });
});
});
You can use array of middlewares
const { wrapRouter } = require('express-async-await');
const router = wrapRouter(express.Router());
router.get('/foo', [
async (req, res, next) => {
next();
},
async (req, res, next) => {
throw new Error('Exception!');
}
]);
router.get('/boo', [
async (req, res, next) => {
next();
},
async (req, res, next) => {
await UserService.find();
}
]);
You can use middleware without async/await
const { wrapRouter } = require('express-async-await');
const router = wrapRouter(express.Router());
router.get('/foo', [
(req, res, next) => {
next();
},
(req, res, next) => {
try {
res.send();
} catch (err) {
next(err);
}
}
]);
API
wrapRouter()
The wrapRouter()
is the best way to add async/await
support to your Express app or Router.
wrap()
If you need more control you can use the wrap()
function.
This function wraps an async Express middleware and adds async/await support.