Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I get an ESLint warning:
Expected to return a value art the end of arrow function ( consistent-return)
errors.details.forEach((error) => {
const errorExists = find(errObj, (item) => { // <== ESLint warning
if (item && item.field === error.path && item.location === location) {
item.messages.push(error.message);
item.types.push(error.type);
return item;
if (!errorExists) {
errObj.push({
field: error.path,
location: error.location,
messages: [error.message],
types: [error.type]
However if I insert a return
const errorExists = find(errObj, (item) => { // <== ESLint warning
if (item && item.field === error.path && item.location === location) {
item.messages.push(error.message);
item.types.push(error.type);
return item;
return; // <== inserted return
Then no more warning on this line , but then I get 2 warnings on the inserted return ...
Arrow function expected a return value (consistently-return)
Unnecessary return statement (no-useless-return)
I don't see how to solve correctly this issue ..
any feedback welcome
http://eslint.org/docs/rules/consistent-return says:
This rule requires return statements to either always or never specify values.
When your if-condition is not met, the arrow function will terminate without encountering a return statement, which violates this rule. Your second version violates this rule because your second return does not specify a value, contrary to the first return. The second warning tells you that your additional return statement is redundant.
To make the linter happy, you probably should think about what to properly return from the arrow-function if the condition is not met. I do not know what your find function does exactly, but if it behaves similar to Array.prototype.find you might want to return false at the end of the arrow function. If you need to return undefined in that case, this paragraph from the same page applies:
When Not To Use It
If you want to allow functions to have different return behavior depending on code branching, then it is safe to disable this rule.
EDIT: I previously wrote to have a look at the option treatUndefinedAsUnspecified, but looks like either setting will not help if you need to return undefined in just one of the branches.
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.