@typescript-eslint/promise-function-async
Require functions that return a promise to be async
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.
[lint.rules]
"@typescript-eslint/promise-function-async" = "error" # or "warn"Every option, set to its default:
[lint.rules]
"@typescript-eslint/promise-function-async" = ["error", {
allowAny = true,
allowedPromiseNames = [], # list of strings
checkArrowFunctions = true,
checkFunctionDeclarations = true,
checkFunctionExpressions = true,
checkMethodDeclarations = true,
}]Examples#
function loadUserFunctions that return promises must be async.(id: string) {
if (!id) throw new Error("missing id");
return fetch(`/users/${id}`);
}
const cached = (hit?: string) =>Functions that return promises must be async. Consider adding an explicit return type annotation if the function is intended to return a union of promise and non-promise types. hit ?? Promise.resolve("miss");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:
{ "allowAny": false }1 problemfunction parseFunctions that return promises must be async.(text: string): unknown {
return JSON.parse(text);
}Reported with allowedPromiseNames: ["Task"]:
{ "allowedPromiseNames": ["Task"] }1 problemclass Task {}
function startFunctions that return promises must be async.() {
return new Task();
}Not reported with checkFunctionExpressions: false:
{ "checkFunctionExpressions": false }No problemsconst api = {
ping() {
return fetch("/ping");
},
};