1 | # `promise-function-async`
|
2 |
|
3 | Requires any function or method that returns a Promise to be marked async.
|
4 |
|
5 | Requires any function or method that returns a Promise to be marked async.
|
6 | Ensures that each function is only capable of:
|
7 |
|
8 | - returning a rejected promise, or
|
9 | - throwing an Error object.
|
10 |
|
11 | In contrast, non-`async` `Promise` - returning functions are technically capable of either.
|
12 | Code that handles the results of those functions will often need to handle both cases, which can get complex.
|
13 | This rule's practice removes a requirement for creating code to handle both cases.
|
14 |
|
15 | ## Rule Details
|
16 |
|
17 | Examples of code for this rule
|
18 |
|
19 |
|
20 |
|
21 | ### ❌ Incorrect
|
22 |
|
23 | ```ts
|
24 | const arrowFunctionReturnsPromise = () => Promise.resolve('value');
|
25 |
|
26 | function functionReturnsPromise() {
|
27 | return Promise.resolve('value');
|
28 | }
|
29 | ```
|
30 |
|
31 | ### ✅ Correct
|
32 |
|
33 | ```ts
|
34 | const arrowFunctionReturnsPromise = async () => Promise.resolve('value');
|
35 |
|
36 | async function functionReturnsPromise() {
|
37 | return Promise.resolve('value');
|
38 | }
|
39 | ```
|
40 |
|
41 | ## Options
|
42 |
|
43 | Options may be provided as an object with:
|
44 |
|
45 | - `allowAny` to indicate that `any` or `unknown` shouldn't be considered Promises (`true` by default).
|
46 | - `allowedPromiseNames` to indicate any extra names of classes or interfaces to be considered Promises when returned.
|
47 |
|
48 | In addition, each of the following properties may be provided, and default to `true`:
|
49 |
|
50 | - `checkArrowFunctions`
|
51 | - `checkFunctionDeclarations`
|
52 | - `checkFunctionExpressions`
|
53 | - `checkMethodDeclarations`
|
54 |
|
55 | ```json
|
56 | {
|
57 | "@typescript-eslint/promise-function-async": [
|
58 | "error",
|
59 | {
|
60 | "allowedPromiseNames": ["Thenable"],
|
61 | "checkArrowFunctions": true,
|
62 | "checkFunctionDeclarations": true,
|
63 | "checkFunctionExpressions": true,
|
64 | "checkMethodDeclarations": true
|
65 | }
|
66 | ]
|
67 | }
|
68 | ```
|
69 |
|
70 | ## Related To
|
71 |
|
72 | - TSLint: [promise-function-async](https://palantir.github.io/tslint/rules/promise-function-async)
|
73 |
|
74 | ## Attributes
|
75 |
|
76 | - Configs:
|
77 | - [ ] ✅ Recommended
|
78 | - [ ] 🔒 Strict
|
79 | - [x] 🔧 Fixable
|
80 | - [x] 💭 Requires type information
|