Skip to main content

promise-function-async

Require any function or method that returns a Promise to be marked async.

Ensures that each function is only capable of:

  • returning a rejected promise, or
  • throwing an Error object.

In contrast, non-async Promise - returning functions are technically capable of either. Code that handles the results of those functions will often need to handle both cases, which can get complex. This rule's practice removes a requirement for creating code to handle both cases.

Attributes

  • Included in configs
    • ✅ Recommended
    • 🔒 Strict
  • Fixable
    • 🔧 Automated Fixer
    • 💡 Suggestion Fixer
  • 💭 Requires type information

Rule Details

Examples of code for this rule

const arrowFunctionReturnsPromise = () => Promise.resolve('value');

function functionReturnsPromise() {
return Promise.resolve('value');
}

Options

.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/promise-function-async": "warn"
}
};

This rule accepts an options object with the following properties:

interface Options {
/**
* Whether to consider `any` and `unknown` to be Promises.
*/
allowAny?: boolean;
/**
* Any extra names of classes or interfaces to be considered Promises.
*/
allowedPromiseNames?: string[];
checkArrowFunctions?: boolean;
checkFunctionDeclarations?: boolean;
checkFunctionExpressions?: boolean;
checkMethodDeclarations?: boolean;
}

const defaultOptions: Options = [
{
allowAny: true,
allowedPromiseNames: [],
checkArrowFunctions: true,
checkFunctionDeclarations: true,
checkFunctionExpressions: true,
checkMethodDeclarations: true,
},
];

Resources