2

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?

uzyn
  • 6,625
  • 5
  • 22
  • 41

2 Answers2

2

You do not return a value from within this if block:

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

This means that the function will return undefined if condition is true, but return the result of the resolve function, if false.

Matt
  • 314
  • 2
  • 10
  • Oh that makes sense, can you suggest on how to fix it? – uzyn Sep 10 '16 at 09:08
  • 1
    For sure, just add a return value, that does depend on what you're using this for but it will probably work to just `return asyncMethod.then(data => { return resolve(syncMethod()); }, err => reject(err));` – Matt Sep 10 '16 at 09:11
1

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.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375