@typescript-eslint/promise-function-async

Require functions that return a promise to be async

fixableneeds types

Also called typescript-eslint/promise-function-async, typescript/promise-function-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.

Configure#

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

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

Every option, set to its default:

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

Examples#

example.ts2 problems
function loadUser(id: string) {
  if (!id) throw new Error("missing id");
  return fetch(`/users/${id}`);
}

const cached = (hit?: string) => hit ?? Promise.resolve("miss");
example.tsNo problems
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#

OptionDefaultWhat it does
allowAnytrueWith false, also report functions that return any or unknown.
allowedPromiseNames[]Names of other classes or interfaces to treat as promises.
checkArrowFunctionstrueCheck arrow functions.
checkFunctionDeclarationstrueCheck function declarations.
checkFunctionExpressionstrueCheck function expressions and object literal methods.
checkMethodDeclarationstrueCheck class methods.

Reported with allowAny: false:

example.tswith { "allowAny": false }1 problem
function parse(text: string): unknown {
  return JSON.parse(text);
}

Reported with allowedPromiseNames: ["Task"]:

example.tswith { "allowedPromiseNames": ["Task"] }1 problem
class Task {}

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

Not reported with checkFunctionExpressions: false:

example.tswith { "checkFunctionExpressions": false }No problems
const api = {
  ping() {
    return fetch("/ping");
  },
};