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.

consistent-return for async functions

See original GitHub issue

Tell us about your environment

  • ESLint Version: v5.5.0
  • Node Version: v10.10.0
  • npm Version: v6.4.1

What parser (default, Babel-ESLint, etc.) are you using?

default

Please show your full configuration:

I just have consistent-return set to error.

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

async function test(options) {
	try {
		await otherPromiseFunc();
	} catch (error) {
		return Promise.reject(new Error('err'));
	}
}

What did you expect to happen?

The async function has an implicit return of a Promise. The return statement inside the catch block is used to pass the error along to the higher level catchers as if we don’t it will be lost here.

What actually happened? Please include the actual, raw output from ESLint.

Currently how the rule is defined it will flag that the main test function needs an outer return basically to keep it consistent, but as we already have async implicit return for a Promise I think this should be skipped.

Maybe not updating the rules as is, but add a flag for async functions?

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
not-an-aardvarkcommented, Sep 7, 2018

Hi, thanks for creating an issue.

Could the code be written like this instead?

async function test(options) {
	try {
		await otherPromiseFunc();
	} catch (error) {
		throw new Error('err');
	}
}

It seems like that would be a clearer way to write the code that doesn’t violate the consistent-return rule.

1reaction
not-an-aardvarkcommented, Sep 7, 2018

I don’t think that’s correct – throwing an error in an async function has the effect of returning a rejected Promise.

Read more comments on GitHub >

github_iconTop Results From Across the Web

consistent-return - 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 >
Async arrow function expected no return value - Stack Overflow
The error you're having says consistent-return at the end, which basically means your function should consistently return the same type.
Read more >
async function - JavaScript - MDN Web Docs - Mozilla
Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly...
Read more >
return-await | typescript-eslint
Enforce consistent returning of awaited values. Some problems reported by this rule are automatically fixable by the --fix ESLint command line option.
Read more >
Consistent Return Rule - JavaScript ESLint | MojoTech
When you enable the consistent-return rule in ESLint the tool will automatically check that all code paths in a function either explicitly ...
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