ESLint consistent-return Promise 错误

ESLint consistent-return error with Promise

我在以下代码的第一行得到一个 consistent-return ESLint error

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

return 不一致的遗漏情况是什么?应该如何解决?

您没有return这个if块中的值:

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

这意味着函数将 return undefined 如果 condition 为真,但 return 解析函数的结果,如果为假。

ESlint 没有捕捉到这一点,但你应该 avoid the Promise constructor 完全!

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

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

您没有从 promise 构造函数的 if 块中 returning 任何内容。或者更确切地说,您不应该 return 编辑 else 块中 resolve() 调用的结果。