# unicorn/no-useless-promise-resolve-reject

> Disallow returning `Promise.resolve()` or `Promise.reject()` from an async function or promise callback

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`.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"unicorn/no-useless-promise-resolve-reject" = "error" # or "warn"
```

Every option, set to its default:

```toml bunfig.toml
[lint.rules]
"unicorn/no-useless-promise-resolve-reject" = ["error", {
  allowReject = false,
}]
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 3} */}
```js
async function findUser(id) {
  if (!id) {
    return Promise.reject(new Error("missing id"));
  }
  return Promise.resolve(cache.get(id));
}

fetchUser().then(user => Promise.resolve(user.name));
```

{/* bad */}
```js
async function* pages() {
  yield Promise.resolve(firstPage);
}
```

Not reported:

{/* good */}
```js
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. |

{/* good {"options": [{"allowReject": true}]} */}
```js
async function findUser(id) {
  return Promise.reject(new Error("not found"));
}
```
