question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule Suggestion: Prefer Arrow Callback

See original GitHub issue

A rule for ES6 to recommend use of arrow functions in callback

Warnings when Arrow function is preferred

func( function () {...} )
setTimeout(function () {...}, 3000);

Not Warnings when Arrow function is preferred

func( () => {...} )
setTimeout( () => {...}, 3000);

Warnings when standard es5 function is preferred

func( () => {...} )
setTimeout( () => {...}, 3000);

Not Warnings when standard es5 function is preferred

func( function () {...} )
setTimeout(function () {...}, 3000);

Rule Example

"prefer-arrow-callback": [2, "always"]

or rule could alternatively look like:

"callback-style": [2, "arrow"]

ref conversation in #3113

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
pigullacommented, Aug 28, 2015

I think it would be a good idea to add an option to this to only enforce arrow functions for single expressions (i.e., like lambdas in Python).

"prefer-arrow-callback": [2, "except-mutliple-statements"]

With that option set, these would be considered warnings:

foo.forEach(function (item) { return item.toString(); });
foo.forEach(item => { item.bar(); return item.toString(); });

and these would be okay:

foo.forEach(item => item.toString());
foo.forEach(function (item) { item.bar(); return item.toString(); });
0reactions
ilyavolodincommented, Aug 12, 2015

@mysticatea I don’t think so. Let’s hold of on opposite style until somebody requests it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

prefer-arrow-callback - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
eslint-browser/prefer-arrow-callback.md at master - GitHub
This rule is aimed to flag usage of function expressions in an argument list. The following patterns are considered problems: /*eslint prefer-arrow-callback: " ......
Read more >
prefer-arrow-callback - ESLint Config
This rule locates function expressions used as callbacks or function arguments. An error will be produced for any that could be replaced by...
Read more >
es-lint rule to force arrow functions as callbacks - Reddit
prefer -arrow-callback doesn't seem to do the trick. Interestingly I found this https://github.com/sindresorhus/eslint-plugin-unicorn that ...
Read more >
ESLINT: functionseslint(prefer-arrow/prefer-arrow-functions) in ...
Can anyone suggest a solution? I have seen some ideas here: https://eslint.org/docs/rules/prefer-arrow-callback but I am not sure I know how ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found