UNPKG

2 kBMarkdownView Raw
1# `promise-function-async`
2
3Requires any function or method that returns a Promise to be marked async.
4
5Requires any function or method that returns a Promise to be marked async.
6Ensures that each function is only capable of:
7
8- returning a rejected promise, or
9- throwing an Error object.
10
11In contrast, non-`async` `Promise` - returning functions are technically capable of either.
12Code that handles the results of those functions will often need to handle both cases, which can get complex.
13This rule's practice removes a requirement for creating code to handle both cases.
14
15## Rule Details
16
17Examples of code for this rule
18
19<!--tabs-->
20
21### ❌ Incorrect
22
23```ts
24const arrowFunctionReturnsPromise = () => Promise.resolve('value');
25
26function functionReturnsPromise() {
27 return Promise.resolve('value');
28}
29```
30
31### ✅ Correct
32
33```ts
34const arrowFunctionReturnsPromise = async () => Promise.resolve('value');
35
36async function functionReturnsPromise() {
37 return Promise.resolve('value');
38}
39```
40
41## Options
42
43Options 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
48In 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