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

Fat arrow doesn't work for async methods #4349

Closed
al6x opened this issue Oct 30, 2016 · 2 comments
Closed

Fat arrow doesn't work for async methods #4349

al6x opened this issue Oct 30, 2016 · 2 comments

Comments

@al6x
Copy link

al6x commented Oct 30, 2016

The binding for fat arrow is lost if you wrap class method with some function. I demonstrate it with async example, but it's not tied to it, the behaviour will be the same for any other function too.

class Events
  handle: async (event) =>
    console.log @

Will produce (note the this is lost, actually it's not even lost but completely wrong, the Events is used instead of this):

var Events;

Events = (function() {
  function Events() {}

  Events.prototype.handle = async(function(event) {
    return console.log(Events);
  });

  return Events;
})();

It should produce:

var Events,
  bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

Events = (function() {
  function Events() {
    this.handle = bind(this.handle, this);
  }

  Events.prototype.handle = async(function(event) {
    return console.log(this);
  });

  return Events;

})();
@al6x al6x changed the title Wrong class code-generation for async methods Fat arrow method doesn't work for async methods Oct 30, 2016
@al6x al6x changed the title Fat arrow method doesn't work for async methods Fat arrow doesn't work for async methods Oct 30, 2016
@thinkingofmaud
Copy link

thinkingofmaud commented Oct 31, 2016

That's intended. The fat arrow syntax expresses a function that inherits the context from the outer expression.

Inside a class declaration, the context is the class itself.

I think the syntax for

class Events
  handle: =>
    #inside the method, 'this' is not the outer context (Events) but the Events instance.
    console.log this

is wrong, but that should break some old code.

assuming async is a decorator function, you may write your code as

class Events
  handle: -> do async (event) =>
    console.log @

so the fat arrow expression is written inside the instance context, that I assume is the one you want to pass along.

@GeoffreyBooth
Copy link
Collaborator

The title of your issue is confusing. This isn’t really an “async method”, it just happens to be that you’re passing a method as an argument to a function named async.

We’re working on true async support in #3757.

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

3 participants