unicorn/no-useless-promise-resolve-reject
Disallow returning Promise.resolve() or Promise.reject() from an async function or promise callback
fixable
An async function wraps whatever it returns in a promise, and turns anything it throws into a rejection. Callbacks passed to .then(), .catch() and .finally() work the same way. Returning Promise.resolve(value) or Promise.reject(error) from them wraps a second time for nothing. return value and throw error do the same thing.
yield Promise.resolve(value) in an async generator is reported too.
--fix rewrites to return value or throw error. It doesn't touch a Promise.reject() inside a try, because that try would catch the throw.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"unicorn/no-useless-promise-resolve-reject" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"unicorn/no-useless-promise-resolve-reject" = ["error", {
allowReject = false,
}]Examples#
example.js3 problems
async function findUser(id) {
if (!id) {
return Promise.reject(new Error("missing id"))Prefer throw error over return Promise.reject(error).;
}
return Promise.resolve(cache.get(id))Prefer return value over return Promise.resolve(value).;
}
fetchUser().then(user => Promise.resolve(user.name)Prefer return value over return Promise.resolve(value).);example.js1 problem
async function* pages() {
yield Promise.resolve(firstPage)Prefer yield value over yield Promise.resolve(value).;
}example.jsNo problems
async function findUser(id) {
if (!id) {
throw new Error("missing id");
}
return cache.get(id);
}
fetchUser().then(user => user.name);
// not async: the caller needs the promise
function findCached(id) {
return Promise.resolve(cache.get(id));
}Options#
| Option | Default | What it does |
|---|---|---|
allowReject | false | Allow Promise.reject(). Promise.resolve() is still reported. |
example.jswith
{ "allowReject": true }No problemsasync function findUser(id) {
return Promise.reject(new Error("not found"));
}