error Expected the Promise rejection reason to be an Error (prefer-promise-reject-errors)

栏目: eslint 发布时间:2021-12-24

先来看一个 eslint 报错信息:

error: Expected the Promise rejection reason to be an Error (prefer-promise-reject-errors) at src/views/pages/account/login.vue:42:16:
  40 |     const validatePass = async(rule: RuleObject, value: string) => {
  41 |       if (value === '') {
> 42 |         return Promise.reject('Please input the password');
     |                ^
  43 |       }
  44 |       if (formState.checkPass !== '') {
  45 |         formRef.value.validateField('checkPass');

prefer-promise-reject-errors 规则

该规则旨在确保 Promise 只使用 Error 对象拒绝

/*eslint-env es6*/

// Bad
a => {}

// Good
(a) => {}

Options

该规则有一个可选的对象参数:

allowEmptyReject: true (默认为 false) 允许调用不带参数的 Promise.reject()。

假如我想把这个规则禁掉,可以这样配置

// .eslintrc.js
module.exports = {
  rules: {
    'prefer-promise-reject-errors': 0
  },
};

本文地址:https://www.tides.cn/p_eslint-prefer-promise-reject-errors