Koa-router 请求异步问题
-
参考: 地址
-
koa-router源码: 地址
-
通过promise实现
router.get('/double', function(ctx, next) { return new Promise(function(resolve, reject) {//关键啊,文档中居然没有 setTimeout(function() { ctx.body = {message: 'Hello'}; //这就是我遇到的问题啊。异步中的ctx.body赋值。 resolve(next()); }, 1); }); }, function(ctx, next) { return new Promise(function(resolve, reject) { setTimeout(function() { ctx.body.message += ' World'; resolve(next()); }, 1); }); }, function(ctx, next) { ctx.body.message += '!'; });
文章评论