之前在项目里,使用了很多co的代码,co.wrap 看上去不是那么可读性。 同时也尝试了babel和typescript, 那种compile一次也是比较烦。
在node 7.6+中,全面支持 async/await。在项目里,我用的比较多了是基于洋葱模型的middleware - microloom
app.use(async function* (ctx, next) {
ctx.arr.push(1)
await next()
await wait(1)
ctx.arr.push(4)
return ctx;
});
app.use(async function* (ctx, next) {
ctx.arr.push(2)
await next()
ctx.arr.push(3)
});
app.handle({
arr: [0]
}).then(function (result) {
console.log(result);
}).catch(function (e) {
console.error(e)
});