简体   繁体   中英

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

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;
    }
  }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM