Search code examples
javascriptecmascript-6es6-promiseeslint

ESLint consistent-return error with Promise


I'm getting a consistent-return ESLint error on the first line of the following code.

return new Promise((resolve, reject) => { 
  if (condition) {
    asyncMethod.then(data => {
      return resolve(syncMethod());
    }, err => reject(err));
  } else {
    return resolve(syncMethod());
  }
});

What is the the missing case where the return is not consistent and how should it be fixed?


Solution

  • ESlint doesn't catch this, but you should avoid the Promise constructor altogether!

    return (condition ? asyncMethod : Promise.resolve()).then(data => syncMethod());
    

    What is the the missing case where the return is not consistent?

    You weren't returning anything from the if block in the promise constructor. Or rather, you shouldn't have returned the result of the resolve() call in the else block.