prefer-promise-reject-errors

要求使用 Error 对象作为 Promise 拒绝原因

对于 Promises 中的用户定义错误,仅将内置 Error 对象的实例传递给 reject() 函数被认为是一种很好的做法。Error 对象自动存储堆栈跟踪,可用于通过确定错误来自何处来调试错误。如果 Promise 以非 Error 值被拒绝,则可能很难确定拒绝发生的位置。

规则详情

该规则旨在确保 Promises 仅被 Error 对象拒绝。

选项

此规则采用一个可选对象参数:

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

OpUIv0oisK0bLUHx1HrbeprjTmaHIkOBU8JijIRILjf97h20WuzOboUCkSVDjgDx

/*eslint prefer-promise-reject-errors: "error"*/

Promise.reject("something bad happened");

Promise.reject(5);

Promise.reject();

new Promise(function(resolve, reject) {
  reject("something bad happened");
});

new Promise(function(resolve, reject) {
  reject();
});

cpCXNsC2RixCLVqfAZWyxxZwdKAQQJ9XuumnZbwI+QzJDz9EkFlmFnrpIlN7+8w0

/*eslint prefer-promise-reject-errors: "error"*/

Promise.reject(new Error("something bad happened"));

Promise.reject(new TypeError("something bad happened"));

new Promise(function(resolve, reject) {
  reject(new Error("something bad happened"));
});

var foo = getUnknownValue();
Promise.reject(foo);

CDruO8STtfiPEOKFpJL7OFmL2Qa+Gmjj9IocQHbiKP8KG0lSz4WG1iHwq6/mcnCA5rSG2nl9Q7XhIs9SfRLN3BqPGPW9wYuFWLM949xT+tOZYVpztHN0UkmsczV+tiYR

/*eslint prefer-promise-reject-errors: ["error", {"allowEmptyReject": true}]*/

Promise.reject();

new Promise(function(resolve, reject) {
  reject();
});

已知限制

7Nb/PJvqqVosfdnIT6tcEQj/fIh8FZw7nTXaEIL1Tq+F25607jVB0eTQw/u4GE8ex+eD4HiWdQTWNguUYtDvUz8SmvEzcJ30D/Soy3rY/pE828LrqtEKzHWjKu5o9orUybjTqIl5O0s4EL5xIb/PjOVBxyzOOulosouNiJBwKSMh19gRcOlU1zH+JYo52z1qcvjib51mA96NG8ACyuljqVwLRVpHwdMCCxSJrByoq3kVqw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