Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/danawoodman/express-async-wrapper
Wrap async express routes so exceptions can be caught by middleware.
https://github.com/danawoodman/express-async-wrapper
async asyncawait errorhandling errors express express-js expressjs helper javascript js middleware route routes tool util wrapper
Last synced: about 1 month ago
JSON representation
Wrap async express routes so exceptions can be caught by middleware.
- Host: GitHub
- URL: https://github.com/danawoodman/express-async-wrapper
- Owner: danawoodman
- Created: 2017-04-08T15:17:24.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-04-14T00:56:27.000Z (over 7 years ago)
- Last Synced: 2024-12-08T20:36:40.707Z (about 1 month ago)
- Topics: async, asyncawait, errorhandling, errors, express, express-js, expressjs, helper, javascript, js, middleware, route, routes, tool, util, wrapper
- Language: JavaScript
- Homepage:
- Size: 2.93 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# express-async-wrapper
> Wrap async express routes so exceptions can be caught by middleware.
If you're using the awesome ES7/ES2017 `async`/`await` features in your express routes, you'll notice that when a route throws an exception it times out. This wrapper allows express to properly catch exceptions and pass them to the appropriate error handler instead.
This code was borrowed from the [StrongLoop blog post](https://strongloop.com/strongblog/async-error-handling-expressjs-es7-promises-generators/#usinges7asyncawait) on handling `async`/`await` routes in express, I take no credit for coming up with this solution, I just wanted an npm module to use in my apps.
## Install
```bash
# With npm
npm install --save express-async-wrapper# With yarn
yarn add express-async-wrapper
```## Usage
If you have a `async` express route file, like so:
```js
const wrap = require('express-async-wrapper')module.exports = wrap(async (req, res) => {
const msg = await someSlowNetworkThingy()
res.send('Hello', msg)
})
```Now your route properly returns a promise object for express that will catch any throw exceptions.
## Changelog
### v0.1.0
- Initial release! 🎉 🍾
## Credits
Licensed under an MIT license by [Dana Woodman](http://danawoodman.com).
Original source code by StrongLoop (see [here](https://strongloop.com/strongblog/async-error-handling-expressjs-es7-promises-generators/#usinges7asyncawait))
Pull requests welcome!