consistent-return for async functions
See original GitHub issueTell 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:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
Hi, thanks for creating an issue.
Could the code be written like this instead?
It seems like that would be a clearer way to write the code that doesn’t violate the
consistent-return
rule.I don’t think that’s correct – throwing an error in an async function has the effect of returning a rejected Promise.