Skip to content

promise/no-return-wrap Style ​

🚧 An auto-fix is still under development.

What it does ​

Prevents unnecessary wrapping of return values in promises with either Promise.resolve or Promise.reject.

This rule enforces the following stances:

  1. When a promise is to be resolved, instead of returning Promise.resolve(value) it is better to return the raw value with return value instead.

  2. When a promise is to be rejected, instead of returning Promise.reject(error), instead the raw error value should be thrown as in throw error.

There is an option to turn off the enforcing of 2, see the options section below.

Why is this bad? ​

It is unnecessary to use Promise.resolve and Promise.rejectfor converting raw values to promises in the return statements ofthenandcatchcallbacks. Using these operations to convert raw values to promises is unnecessary as simply returning the raw value for the success case and throwing the raw error value in the failure case have the same effect. This is why some take the opinion that returning values such asPromise.resolve(1)orPromise.reject(err)` is syntactic noise.

Examples ​

Examples of incorrect code for this rule:

js
myPromise().then(() => Promise.resolve(4));
myPromise().then(function () {
  return Promise.resolve(4);
});

myPromise().then(() => Promise.reject("err"));
myPromise().then(function () {
  return Promise.reject("err");
});
js
myPromise().catch(function () {
  return Promise.reject("err");
});
js
myPromise().finally(function () {
  return Promise.reject("err");
});
js
myPromise().finally(() => Promise.resolve(4));

Examples of correct code for this rule:

js
myPromise().then(() => 4);
myPromise().then(function () {
  return 4;
});

myPromise().then(() => throw "err");
myPromise().then(function () {
  throw "err";
});
js
myPromise().catch(function () {
  throw "err";
});
js
myPromise().finally(() => 4);

Options ​

allowReject ​

{ type: boolean, default: false }

The allowReject turns off the checking of returning a call Promise.reject inside a promise handler.

With allowReject set to true the following are examples of correct code:

js
myPromise().then(function () {
  return Promise.reject(0);
});
js
myPromise()
  .then()
  .catch(() => Promise.reject("err"));

How to use ​

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny promise/no-return-wrap --promise-plugin
json
{
  "plugins": ["promise"],
  "rules": {
    "promise/no-return-wrap": "error"
  }
}

References ​

Released under the MIT License.