Tuesday, 4 April 2017

ESLint Consistent-return Error, but why?

I've built the following function which routes an email to a user or an admin based on defined object properties passed into the function. When running this through ESLint I'm seeing the overall function is popping up information stating:

" Expected to return a value at the end of function 'emailRouter'. (consistent-return) "

I've read through ESLint docs which as the error suggests is regarding the non consistent return values. When reviewing my code I can't see myself why this is the case. I return a callback through each of my routes. If I add a return to the final sendEmail function then this error goes away, but doesn't seem to be the correct approach.

Any advice? It may be a case of starting at the same function for too long and having a code whiteout.

const emailRouter = function emailRouter(router, callback) {
  if (typeof router !== 'object') {
    return callback(new Error('No routing object given.', null));
  }

    // Mail is for admin
  if (router.direction === 'admin') {
    console.info(`Send to admin due to: ${router.reason}`);
// eslint-disable-next-line max-len
    sendEmail(config.admin.email, config.admin.name, router.reason, router.email.body, (err, result) => {
      if (err) {
        return callback(err, null);
      }
      return callback(null, result);
    });
  }

  // Mail is for a user
  if (typeof router.useremail !== 'string' && router.useremail.length() < 1) {
      // User hasn't got an email set. Forward the notification to channel?
    return callback(new Error('User does not have an email address, nothing to mail.'), null);
  }

// eslint-disable-next-line max-len
  sendEmail(router.useremail, router.user, router.email.subject, router.email.body, (err, result) => {
    if (err) {
      return callback(err, null);
    }
    return callback(null, result);
  });
};



via munkee

No comments:

Post a Comment