Skip to content

Fat arrow doesn't work for async methods #4349

@al6x

Description

@al6x

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;

})();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions