IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    终于不用co了,开始使用async/await

    Samurais发表于 2017-03-03 15:35:30
    love 0

    之前在项目里,使用了很多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)
        });
    


沪ICP备19023445号-2号
友情链接