promise/no-return-wrap
Disallow returning Promise.resolve() or Promise.reject() from a promise callback
then() and catch() already wrap whatever their callback returns in a promise, and an error thrown in the callback already rejects the chain. So return Promise.resolve(value) does the same as return value, and return Promise.reject(error) does the same as throw error.
The rule checks the return values of then(), catch() and finally() callbacks.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"promise/no-return-wrap" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"promise/no-return-wrap" = ["error", {
allowReject = false,
}]Examples#
example.js2 problems
fetchUser(id).then(user => {
if (!user.active) {
return Promise.reject(new Error("inactive user"))Expected throw instead of Promise.reject;
}
return Promise.resolve(user.profile)Avoid wrapping return values in Promise.resolve;
});example.jsNo problems
fetchUser(id).then(user => {
if (!user.active) {
throw new Error("inactive user");
}
return user.profile;
});Outside a promise callback, both are fine:
example.jsNo problems
function cachedUser(id) {
return Promise.resolve(cache.get(id));
}Options#
| Option | Default | What it does |
|---|---|---|
allowReject | false | Allow return Promise.reject(error). Promise.resolve() is still reported. |
example.jswith
{ "allowReject": true }No problemsfetchUser(id).catch(err => Promise.reject(new Error("no user", { cause: err })));example.jswith
{ "allowReject": true }1 problemfetchUser(id).then(user => Promise.resolve(user.profile)Avoid wrapping return values in Promise.resolve);