I'm using express with node and want to use a co/yield patter to wrangle my async callbacks.
The current code looks like this:
web.post('/request/path', function(req, res, next) {
co(function *() {
let body = req.body
let account = yield db.get('account', {key: body.account})
if (!account) {
throw new Error('Cannot find account')
}
let host = yield db.get('host', {key: body.hostname})
....
}).catch(err => {log.info(err) ; res.send({error: err})})
This is working really well, but I'd like to be able to simplify the first 2 lines:
web.post('/request/path', function(req, res, next) {
co(function *() {
Is it possible to somehow integrate the co(function *() into the first line? Does express provide support for co() and yielding functions?
You can use co-express along with promises.
Example,