Skip to content

Question about code style: arrow function V.S. callback function #14496

@XadillaX

Description

@XadillaX
  • Version: -
  • Platform: -
  • Subsystem: -

I want to discuss the code style of arrow function in Node.js source code.

Maybe most of you think that => can replace all of anonymous callback functions. So Node.js source code includes lots of arrow functions. e. g.

// _http_agent.js
nextTick(asyncId, () => ...);
// _http_client.js
req.once('response', (res) => {

etc.

But IMHO, I think arrow function should only use in some situation that the function is just used as a inline lambda expression even though they have a very convenient feature - value of this. That means arrow function should not be misused. And anonymous function should used as callback (mainly asynchronous) or listener functions.

So that's my opinion:

array.map([ ... ], val => { /* do some thing */ });
array.reduce([ ... ], (ans, val) => { ... }, {});

req.on('response', function() {
  // do something
});

fs.readFile(filename, function() {
  // do something
});

I think we can discuss with this and if my opinion is acceptable, I can start working with modifying the style.

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussIssues opened for discussions and feedbacks.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions