如何在另一个生成器函数中访问koa上下文?

Fel*_*lix 5 node.js koa

在Koa中,我可以通过以下方式访问第一个生成器函数中的Koa Context this:

app.use(function *(){
    this; // is the Context
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我屈服于另一个生成器函数,我就无法再访问上下文this了.

app.use(function *(){
    yield myGenerator();
}

function* myGenerator() {
    this.request; // is undefined
}
Run Code Online (Sandbox Code Playgroud)

我已经能够简单地将上下文传递给第二个生成器函数,但是想知道是否有更简洁的方法来访问上下文.

有任何想法吗?

Vin*_*nto 12

要么this像你说的那样作为论据传递:

app.use(function *(){
    yield myGenerator(this);
});

function *myGenerator(context) {
    context.request;
}
Run Code Online (Sandbox Code Playgroud)

或使用apply():

app.use(function *(){
    yield myGenerator.apply(this);
});

function *myGenerator() {
    this.request;
}
Run Code Online (Sandbox Code Playgroud)