# @typescript-eslint/promise-function-async

> Require functions that return a promise to be `async`

A function that returns a promise but isn't `async` can fail in two ways. It can return a rejected promise, or it can throw before it gets that far. The caller then needs both a `try` and a `.catch()`. An `async` function always returns a promise, so every failure is a rejection.

Without a return type annotation, a function is reported if any part of what it returns is a promise. With one, it's reported only if every part of the annotation is a promise.

`--fix` adds `async`.

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

## Configure

Off by default. This rule needs types, so turning it on makes `bun lint` type check your code first.

```toml bunfig.toml
[lint.rules]
"@typescript-eslint/promise-function-async" = "error" # or "warn"
```

Every option, set to its default:

```toml bunfig.toml
[lint.rules]
"@typescript-eslint/promise-function-async" = ["error", {
  allowAny = true,
  allowedPromiseNames = [], # list of strings
  checkArrowFunctions = true,
  checkFunctionDeclarations = true,
  checkFunctionExpressions = true,
  checkMethodDeclarations = true,
}]
```

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

## Examples

Reported:

{/* bad {"count": 2} */}
```ts
function loadUser(id: string) {
  if (!id) throw new Error("missing id");
  return fetch(`/users/${id}`);
}

const cached = (hit?: string) => hit ?? Promise.resolve("miss");
```

Not reported:

{/* good */}
```ts
async function loadUser(id: string) {
  if (!id) throw new Error("missing id");
  return fetch(`/users/${id}`);
}

const cached = (hit?: string): string | Promise<string> => hit ?? Promise.resolve("miss");
```

## Options

| Option | Default | What it does |
| --- | --- | --- |
| `allowAny` | `true` | With `false`, also report functions that return `any` or `unknown`. |
| `allowedPromiseNames` | `[]` | Names of other classes or interfaces to treat as promises. |
| `checkArrowFunctions` | `true` | Check arrow functions. |
| `checkFunctionDeclarations` | `true` | Check function declarations. |
| `checkFunctionExpressions` | `true` | Check function expressions and object literal methods. |
| `checkMethodDeclarations` | `true` | Check class methods. |

Reported with `allowAny: false`:

{/* bad {"options": [{"allowAny": false}]} */}
```ts
function parse(text: string): unknown {
  return JSON.parse(text);
}
```

Reported with `allowedPromiseNames: ["Task"]`:

{/* bad {"options": [{"allowedPromiseNames": ["Task"]}]} */}
```ts
class Task {}

function start() {
  return new Task();
}
```

Not reported with `checkFunctionExpressions: false`:

{/* good {"options": [{"checkFunctionExpressions": false}]} */}
```ts
const api = {
  ping() {
    return fetch("/ping");
  },
};
```
