Skip to content

Stream Transform with Arrow Functions #2740

Closed
@hughrawlinson

Description

@hughrawlinson

Because this in arrow functions is lexically bound, they cannot be used in providing a transform function to a stream.Transform object, in which this.push is used to return data. See the following example:

transform.prototype._transform = (data, encoding, callback)=>{
  this.push(data); // this.push is undefined because this is
                   //lexically bound and refers to the enclosing scope
  callback();
}

The callback function already accepts the output of the transform as its second argument. The following works:

transform.prototype._transform = function (data, encoding, callback) {
  callback(null,data);
}

In light of the fact that moving forward we're going to see a lot more people using the Arrow Function syntax, perhaps we should consider deprecating this.push(data), or at least changing the docs so that the callback(null,data); pattern is recommended moving forward?

I'd be very happy to submit a PR to the docs if there's any consensus on the right solution for this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionIssues that look for answers.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions