我在 node 上使用 express 并希望使用 co/yield 模式来处理我的异步回调。
当前代码如下所示:
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})})
这工作得很好,但我希望能够简化前两行:
web.post('/request/path', function(req, res, next) {
co(function *() {
是否有可能以某种方式将 co(function *() 集成到第一行? express 是否提供对 co() 和 yielding 函数的支持?