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.

[no-promise-executor-return]: false positive on arrow function

See original GitHub issue

What rule do you want to change?

no-promise-executor-return

Does this change cause the rule to produce more or fewer warnings?

Fewer warnings

How will the change be implemented? (New option, new default behavior, etc.)?

New default behaviour that could be changed via an option

Please provide some example code that this change will affect:

It is a common practice to have arrow functions as executors in Promise. Those cases are written that way because they can be one-liners which is to improve the readability.

E.g. writing a delay function as this:

new Promise((resolve) => setTimeout(resolve, millis));

What does the rule currently do for this code?

It reports Return values from promise executor functions cannot be read

What will the rule do after it’s changed?

If it is an arrow function, I believe it makes sense to suppress the warning. Although, there is a possibility to have a code such as:

const someValue = new Promise((resolve) => {
  // code
  // code
  return someValue; // which is wrong, aggre
})

Are you willing to submit a pull request to implement this change?

Not sure how to do this. It is definitely a false positive, because my intention was not consuming the value at all, but to have a one-liner.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
btmillscommented, Sep 7, 2020

👋 hi @ghaiklor-wix! In the original discussion around adding this rule, we explicitly decided not to make an exception for arrow functions, and you can read more about the rationale there. An arrow function is also shown as an example of incorrect code in the rule docs. For those reasons, I’m 👎 to an option that would allow any expression-bodied arrow functions. In most cases, adding curly braces makes the intent more clear, e.g. new Promise((resolve) => { setTimeout(resolve, millis); });.

However, in the original issue, we also contemplated a future enhancement to allow returning function calls as a shortcut. That option would allow your setTimeout() as written without curly braces. Is that something you’d be interested in @ghaiklor-wix? I could be convinced to be in favor of adding such an option, though definitely not as the default behavior because it is contrary to the intent of the rule. @mdjermanovic since you originally suggested it, what are your current thoughts on an option to allow returning call expressions?

0reactions
mmkalcommented, Nov 23, 2020

@btmills I’d also like to allow expression arrow functions. In projects that use prettier, I’ve tended to see this rule just get turned off, because it expands

new Promise(resolve => { setTimeout(resolve, 1000) })

into

new Promise(resolve => {
  setTimeout(resolve, 1000)
})

And taking up three lines for such a simple thing just feels wrong. It’s not the hugest deal, but it makes codebases more cluttered, so it gets turned off.

I’d like an option. But failing that, what if it’s allowed when the return value is explicitly voided?

new Promise(resolve => void setTimeout(resolve, 1000))

Would the team be open to a PR doing one of these things?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Arrow function expressions - JavaScript - MDN Web Docs
An arrow function expression is a compact alternative to a traditional function expression, with some semantic differences and deliberate ...
Read more >
Typescript arrow functions vs normal functions in interfaces ...
Typescript arrow functions vs normal functions in interfaces type inference giving false positive, leading to runtime error [duplicate].
Read more >
arrow-parens - 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 >
Anomalies in JavaScript arrow functions - LogRocket Blog
Arrow functions behave differently from regular JS functions — think of them ... Here are some results from calls to the average() function:...
Read more >
Arrow Functions in JavaScript: Fat & Concise Syntax - SitePoint
Learn how to use JavaScript arrow functions, understand fat and concise arrow syntax, and what to be aware of when using them in...
Read more >

github_iconTop Related Medium Post

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