Urara-Blog/node_modules/.pnpm-store/v3/files/be/4daec44cc36a89928b5f5b3875ce2067c89df036bee5d924d2658460c5ea197c002dc35bca2fbe4f49e5cee89cc45c140c5eedb43bf18f010b3e4bb5109489
2022-08-14 01:14:53 +08:00

71 lines
1.8 KiB
Text

---
description: 'Require any function or method that returns a Promise to be marked async.'
---
> 🛑 This file is source code, not the primary documentation location! 🛑
>
> See **https://typescript-eslint.io/rules/promise-function-async** for documentation.
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.
## Rule Details
Examples of code for this rule
<!--tabs-->
### ❌ Incorrect
```ts
const arrowFunctionReturnsPromise = () => Promise.resolve('value');
function functionReturnsPromise() {
return Promise.resolve('value');
}
```
### ✅ Correct
```ts
const arrowFunctionReturnsPromise = async () => Promise.resolve('value');
async function functionReturnsPromise() {
return Promise.resolve('value');
}
```
## Options
Options may be provided as an object with:
- `allowAny` to indicate that `any` or `unknown` shouldn't be considered Promises (`true` by default).
- `allowedPromiseNames` to indicate any extra names of classes or interfaces to be considered Promises when returned.
In addition, each of the following properties may be provided, and default to `true`:
- `checkArrowFunctions`
- `checkFunctionDeclarations`
- `checkFunctionExpressions`
- `checkMethodDeclarations`
```json
{
"@typescript-eslint/promise-function-async": [
"error",
{
"allowedPromiseNames": ["Thenable"],
"checkArrowFunctions": true,
"checkFunctionDeclarations": true,
"checkFunctionExpressions": true,
"checkMethodDeclarations": true
}
]
}
```