co
co copied to clipboard
can we capture `this` in out co-ed generator function?
I use co inside my class method function, and lost this
in closures inside co when I call it via a instance...
example code:
const co = require('co')
class Test {
constructor() {
this.id = 3
}
test_with_bind_this() {
co(function* () {
console.log(this.id)
}.bind(this))
}
test() {
co(function* () {
console.log(this.id)
})
}
}
const t = new Test()
t.test()
// output undefined
t.test_with_bind_this()
// output 3
is there a easy way to bind to this
automatically?
I usually do co.call(ctx, function*() {}) which is not very bothering
??
@alsotang Thank you! Sorry, my bad. I've removed my misguiding comment.