Skip to content

eslint/prefer-promise-reject-errors Style

What it does

Require using Error objects as Promise rejection reasons

Why is this bad?

It is considered good practice to only pass instances of the built-in Error object to the reject() function for user-defined errors in Promises. Error objects automatically store a stack trace, which can be used to debug an error by determining where it came from. If a Promise is rejected with a non-Error value, it can be difficult to determine where the rejection occurred.

Options

This rule takes one optional object argument:

  • allowEmptyReject: true (false by default) allows calls to Promise.reject() with no arguments.

Examples

Examples of incorrect code for this rule:

js
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();
});

Examples of correct code for this rule:

js
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);

References

Released under the MIT License.