如何在另一个生成器函数中访问koa上下文?
在Koa中,我可以通过以下方式访问第一个生成器函数中的Koa Context:
app.use(function *(){ this; // is the Context }
但是,如果我屈服于另一个生成器函数,我不能通过this
访问上下文了。
app.use(function *(){ yield myGenerator(); } function* myGenerator() { this.request; // is undefined }
我已经能够简单地将上下文传递给第二个生成器函数,但是想知道是否有更简洁的方式来访问上下文。
有任何想法吗?
要么像你所说的那样把this
作为一个论点:
app.use(function *(){ yield myGenerator(this); }); function *myGenerator(context) { context.request; }
或使用apply()
:
app.use(function *(){ yield myGenerator.apply(this); }); function *myGenerator() { this.request; }