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

Callback binding #25

Open
mappum opened this issue Feb 16, 2016 · 1 comment
Open

Callback binding #25

mappum opened this issue Feb 16, 2016 · 1 comment

Comments

@mappum
Copy link

mappum commented Feb 16, 2016

Maybe it would be useful to mention callback binding, I know that personally was confusing for me in the beginning. For instance, you might expect this to work:

Something.prototype.foo = function (cb) {
  someOtherObj.bar(function (err, res) {
    cb(null, this.value)
  })
}

But actually it will not work since the innermost callback is called from the context of someOtherObj, and this doesn't resolve to the Something instance (so this.value end up being something else).

The solutions are foo(function () { this.value... }.bind(this)), var self = this; foo(function () { self.value... }), or maybe ES6 arrow functions (since they take their context from wherever they were created), () => { this.value... }.

@max-mapper
Copy link
Owner

Oh yea good idea. Ill leave this open for the next time I'm doing work on this

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

2 participants