How to access koa context in another generator function?

In Koa, I can access the Koa Context in the first generator function through this :

 app.use(function *(){ this; // is the Context } 

But if I succumb to another function of the generator, I can no longer access the context through this .

 app.use(function *(){ yield myGenerator(); } function* myGenerator() { this.request; // is undefined } 

I could just pass the context to the second function of the generator, but I wondered if there was a cleaner way to access the context.

Any ideas?

+5
source share
1 answer

Or pass this as an argument, as you said:

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

or use apply() :

 app.use(function *(){ yield myGenerator.apply(this); }); function *myGenerator() { this.request; } 
+12
source

Source: https://habr.com/ru/post/1205824/


All Articles