From 1be333ca316def78078710aa914b748efa52ac6f Mon Sep 17 00:00:00 2001 From: gyson Date: Mon, 24 Aug 2015 00:08:54 +0800 Subject: [PATCH] change respond() to a regular function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit remove `yield* next` in lib/application, which caused annoy `A promise was converted into a generator …` message. benchmark result: * when bench with native Promise, it has no impact for both stable and experimental ones. * when bench with Bluebird, it’s about 5-10% faster than original for both stable and experimental ones. closes #472 --- lib/application.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/application.js b/lib/application.js index 878c7a5..e046726 100644 --- a/lib/application.js +++ b/lib/application.js @@ -113,10 +113,9 @@ app.use = function(fn){ */ app.callback = function(){ - var mw = [respond].concat(this.middleware); var fn = this.experimental - ? compose_es7(mw) - : co.wrap(compose(mw)); + ? compose_es7(this.middleware) + : co.wrap(compose(this.middleware)); var self = this; if (!this.listeners('error').length) this.on('error', this.onerror); @@ -125,7 +124,9 @@ app.callback = function(){ res.statusCode = 404; var ctx = self.createContext(req, res); onFinished(res, ctx.onerror); - fn.call(ctx).catch(ctx.onerror); + fn.call(ctx).then(function () { + respond.call(ctx); + }).catch(ctx.onerror); } }; @@ -173,12 +174,10 @@ app.onerror = function(err){ }; /** - * Response middleware. + * Response helper. */ -function *respond(next) { - yield *next; - +function respond() { // allow bypassing koa if (false === this.respond) return;