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?
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 return
ing anything from the if
block in the promise constructor. Or rather, you shouldn't have return
ed the result of the resolve()
call in the else
block.