user15570208
user15570208

Reputation:

Why i am getting this warning 159:35 warning Expected to return a value at the end of arrow function array-callback-return?

i have this code, i am using find to verify if a element exist:

firstly i was using for like this

for (const file of files) {
if (data.name === proName ) {
        exist = true
      }
}

this code now i have to use filter, find, contains, map what is the best to solve this problem?

lists.find(data => {
  if (
    data.name === proName
  ) {
    return data
  }
})

but i am getting this warning

159:35 warning Expected to return a value at the end of arrow function array-callback-return

what happend?

Upvotes: 0

Views: 29

Answers (1)

Batman
Batman

Reputation: 702

The function doesn't always return. I.e. the else path doesn't have a return in it.

Just do

lists.find(data => data.name === proName)

and it'll infer the data.name === proName as being returned from the lambda, or arrow, function you pass into find.

Otherwise you'll want something like

lists.find(data => {
  if (data.name === proName) {
    return data
  } else { 
    return null;
});

But returning null isn't what find expects; it expects a boolean.

Upvotes: 3

Related Questions