https://github.com/pana/wga
Wrapper for generator and async/await to enable them use in Express or restify
https://github.com/pana/wga
Last synced: 3 months ago
JSON representation
Wrapper for generator and async/await to enable them use in Express or restify
- Host: GitHub
- URL: https://github.com/pana/wga
- Owner: Pana
- License: mit
- Created: 2015-04-29T08:36:50.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-04-30T09:44:26.000Z (over 10 years ago)
- Last Synced: 2025-06-28T06:20:56.423Z (3 months ago)
- Language: JavaScript
- Homepage:
- Size: 148 KB
- Stars: 16
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
wga
=========wga is a small JS function which can wrap a generator or async
function to used as request handlers or middleware in Express.
wga brought modern async write style to ExpressInstalling
----------
```sh
npm install wga --save
```Using with generator
-----
```javascript
var wga = require("wga")
var app = require("express")()app.get("/books", wga(function*(req, res, next) {
var books = yield Book.findAll()
res.send(books)
}))
```If any error been throwed out, they will be passed to `next`, which in
Express's case calls the error handling middleware later on.
If the generator succeeds, the `next` callback will not be called.If you need to, like in middleware handlers, you can always call `next` yourself:
```javascript
app.use(wga(function*(req, res, next) {
var user = yield User.find(req.session.userId)
if (user == null) return next(new Error("Unauthorized"))
req.user = user
next()
}))
```Using with async function
---------
```javascript
app.use(wga(async function(req, res, next) {
var user = await User.find(req.session.userId)
if (user == null) return next(new Error("Unauthorized"))
req.user = user
next()
}))app.get('/', wga(async function (req, res) {
let result = await Promise.resolve('Hello world')
res.send(`${result}\n`)
}))
```License
-------
MIT