Why eslint throw that error, and how can I get rid of it?

Tags: ,



I wrote a function to return sessionStorage data and eslint throw error correlated with the return statement in an arrow function

Expected to return a value at the end of arrow function consistent-return

  const data = sessionStorage.getItem(key);
  if (data) {
    try {
      return EJSON.parse(data);
    } catch (err) {
      console.error('readUnloggedInData', err);
      return false;
    }
  }

Answer

This is pretty simple, ESLint is telling you that the functions might exit without returning, in your case that might happen when data is false, so what you could do is:

  const data = sessionStorage.getItem(key);
  if (data) {
    try {
      return EJSON.parse(data);
    } catch (err) {
      console.error('readUnloggedInData', err);
      return false;
    }
  }
  return;

Just return nothing… i know that js will do that automaticly, but ESLint likes you to write it down specificly, due to the “consistent-return” rule in ESLint



Source: stackoverflow