typescript/promise-function-async Restriction ​
What it does ​
This rule requires any function or method that returns a Promise to be marked as async.
Why is this bad? ​
Functions that return Promises should typically be marked as async to make their asynchronous nature clear and to enable the use of await within them. This makes the code more readable and helps prevent common mistakes with Promise handling.
Examples ​
Examples of incorrect code for this rule:
// Function returning Promise without async
function fetchData(): Promise<string> {
return fetch("/api/data").then(res => res.text());
}
// Method returning Promise without async
class DataService {
getData(): Promise<any> {
return fetch("/api/data").then(res => res.json());
}
}
// Arrow function returning Promise without async
const processData = (): Promise<void> => {
return Promise.resolve();
};Examples of correct code for this rule:
// Async function
async function fetchData(): Promise<string> {
const response = await fetch("/api/data");
return response.text();
}
// Async method
class DataService {
async getData(): Promise<any> {
const response = await fetch("/api/data");
return response.json();
}
}
// Async arrow function
const processData = async (): Promise<void> => {
await someAsyncOperation();
};
// Functions that don't return Promise are fine
function syncFunction(): string {
return "hello";
}
// Functions returning Promise-like but not actual Promise
function createThenable(): { then: Function } {
return { then: () => {} };
}Configuration ​
This rule accepts a configuration object with the following properties:
allowAny ​
type: boolean
default: true
Whether to allow functions returning any type without requiring async.
allowedPromiseNames ​
type: string[]
default: []
A list of Promise type names that are allowed without requiring async. Example: ["SpecialPromise"] to allow functions returning SpecialPromise without async.
checkArrowFunctions ​
type: boolean
default: true
Whether to check arrow functions for missing async keyword.
checkFunctionDeclarations ​
type: boolean
default: true
Whether to check function declarations for missing async keyword.
checkFunctionExpressions ​
type: boolean
default: true
Whether to check function expressions for missing async keyword.
checkMethodDeclarations ​
type: boolean
default: true
Whether to check method declarations for missing async keyword.
How to use ​
To enable this rule in the CLI or using the config file, you can use:
oxlint --type-aware --deny typescript/promise-function-async{
"rules": {
"typescript/promise-function-async": "error"
}
}