Repository navigation
Stream Transform with Arrow Functions #2740
Description
Activity
An issue I hadn't considered:
callback(null,data)should only be called once. Perhapsthis.pushshould be passed into the_transformfunction as another callback for use "0 or more times"?- addedquestionIssues asking questions about Node.js.Issues asking questions about Node.js.
on Sep 8, 2015 Please see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
Arrow Functions are not a replacement for regular functions. This is expected behaviour of the language feature, in this context it will bind to the wrong scope.
You can't propagate
thisstate to Arrow Functions, so they are unsuitable for these parts of our API. Again, this is expected, and you will need to use "regular" Functions. :)@Fishrock123 I understand that it's expected behaviour, I explained that the issue is a result of the expected behaviour in the first line of my issue. My point was that now that ES6 usage is going to increase by a lot, we should perhaps consider finding a way to make this part of the API easier with both styles of functions without breaking existing codebases?
My point was that now that ES6 usage is going to increase by a lot, we should perhaps consider finding a way to make this part of the API easier with both styles of functions without breaking existing codebases?
Why would we want to? Arrow Functions are not a replacement for regular Functions..
or at least changing the docs so that the callback(null,data); pattern is recommended moving forward?
A documentation PR indicating that arrow functions should not be used in this type of situation might be in order. Personally, I'd wait to see if people actually run into this problem frequently or not. But if someone felt strongly and wanted to be proactive about it, that's totally cool too.
syntacticsolutions commented
on Oct 24, 2018 on Oct 24, 2018 · Hidden as off-topicshow commentMore actions
Because
thisin arrow functions is lexically bound, they cannot be used in providing a transform function to a stream.Transform object, in whichthis.pushis used to return data. See the following example:The callback function already accepts the output of the transform as its second argument. The following works:
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 thecallback(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.