use async function or generator function with express
MIT License
use async function or generator function with express
npm i express-modern --save
app.get('/', modern((req, res, next) => { ... }))
app.get('/', modern((err, req, res, next) => { ... }))
app.param('user', modern((req, res, next, user) => { ... }))
const modern = require('express-modern')
const app = express();
app.get('/', modern(function * (req, res, next) {
yield new Promise(r => setTimeout(r, 10));
res.send('generator function');
}));
const res = yield request(app)
.get('/')
.endAsync();
res.text.should.match(/generator/);
the generator function will be wrap via co.wrap
. and the modern
function
will take care of the arity of the handler. see http://expressjs.com/en/guide/error-handling.html
const app = express();
app.get('/', modern(async function(req, res, next) {
await new Promise(r => setTimeout(r, 10));
res.send('async function');
}));
const res = yield request(app)
.get('/')
.endAsync();
res.text.should.match(/async/);
the async function will be called & the rejected value will be passed to next
automatically
const app = express();
app.get('/', modern((req, res, next) => {
res.send('normal function');
}));
const res = yield request(app)
.get('/')
.endAsync();
res.text.should.match(/normal/);
the modern
function does nothing on normal function.
the MIT License http://magicdawn.mit-license.org