Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can we capture this in out co-ed generator function? #274

Open
huan opened this issue May 22, 2016 · 3 comments
Open

can we capture this in out co-ed generator function? #274

huan opened this issue May 22, 2016 · 3 comments

Comments

@huan
Copy link

huan commented May 22, 2016

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?

@luckydrq
Copy link

I usually do co.call(ctx, function*() {}) which is not very bothering

@alsotang
Copy link

image

??

@gunar
Copy link

gunar commented Jun 20, 2016

@alsotang Thank you! Sorry, my bad. I've removed my misguiding comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants