express-wrap-async
Version:
Wrap babel async routes to standard express routes
79 lines (60 loc) • 1.48 kB
Markdown
//travis-ci.org/lsentkiewicz/express-wrap-async.svg?branch=master)](https://travis-ci.org/lsentkiewicz/express-wrap-async)
[](https://codecov.io/gh/lsentkiewicz/express-wrap-async)
```
npm i --save express-wrap-async
```
```js
import wrapAsync from 'express-wrap-async';
```
```js
function routePromise(req, res) {
return new Promise((resolve) => {
res.send({ ok: true });
resolve();
});
}
router.get('/promise', wrapAsync(routePromise));
```
```js
const fakeWait = () => new Promise((resolve) => setTimeout(resolve, 100));
async function asyncRoute(req, res) {
await fakeWait();
res.send({ ok: true });
}
router.get('/async', wrapAsync(asyncRoute));
```
```js
const middlewares = [
async(req, res, next) => {
await fakeWait();
next();
},
(req, res, next) => {
// non async middleware will also work
next();
},
asyncRoute,
];
router.get('/middlewares', wrapAsync(middlewares));
```
```js
async function errorRoute(req, res) {
await fakeWait();
throw new Error('unexpected error');
}
router.get('/standard', wrapAsync(errorRoute));
app.use((err, req, res, next) => {
res.status(500);
res.json({
error: err.message,
});
});
```
MIT License
Copyright (c) 2016 Łukasz Sentkiewicz
[![Build Status](https: