change respond() to a regular function

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
This commit is contained in:
gyson 2015-08-24 00:08:54 +08:00 committed by Jonathan Ong
parent 0b9c032af1
commit 1be333ca31

View file

@ -113,10 +113,9 @@ app.use = function(fn){
*/ */
app.callback = function(){ app.callback = function(){
var mw = [respond].concat(this.middleware);
var fn = this.experimental var fn = this.experimental
? compose_es7(mw) ? compose_es7(this.middleware)
: co.wrap(compose(mw)); : co.wrap(compose(this.middleware));
var self = this; var self = this;
if (!this.listeners('error').length) this.on('error', this.onerror); if (!this.listeners('error').length) this.on('error', this.onerror);
@ -125,7 +124,9 @@ app.callback = function(){
res.statusCode = 404; res.statusCode = 404;
var ctx = self.createContext(req, res); var ctx = self.createContext(req, res);
onFinished(res, ctx.onerror); 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) { function respond() {
yield *next;
// allow bypassing koa // allow bypassing koa
if (false === this.respond) return; if (false === this.respond) return;