You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/eslint-plugin/docs/rules/return-await.mdx
+73-11Lines changed: 73 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -21,23 +21,43 @@ The extended rule is named `return-await` instead of `no-return-await` because t
21
21
## Options
22
22
23
23
```ts
24
-
typeOptions='in-try-catch'|'always'|'never';
24
+
typeOptions=
25
+
|'in-try-catch'
26
+
|'always'
27
+
|'error-handling-correctness-only'
28
+
|'never';
25
29
26
30
const defaultOptions:Options='in-try-catch';
27
31
```
28
32
33
+
The options in this rule distinguish between "ordinary contexts" and "error-handling contexts".
34
+
An error-handling context is anywhere where returning an unawaited promise would cause unexpected control flow regarding exceptions/rejections.
35
+
See detailed examples in the sections for each option.
36
+
37
+
- If you return a promise within a `try` block, it should be awaited in order to trigger subsequent `catch` or `finally` blocks as expected.
38
+
- If you return a promise within a `catch` block, and there _is_ a `finally` block, it should be awaited in order to trigger the `finally` block as expected.
39
+
- If you return a promise between a `using` or `await using` declaration and the end of its scope, it should be awaited, since it behaves equivalently to code wrapped in a `try` block followed by a `finally`.
40
+
41
+
Ordinary contexts are anywhere else a promise may be returned.
42
+
The choice of whether to await a returned promise in an ordinary context is mostly stylistic.
43
+
44
+
With these terms defined, the options may be summarized as follows:
45
+
46
+
| Option | Ordinary Context <br/> (stylistic preference 🎨) | Error-Handling Context <br/> (catches bugs 🐛) | Should I use this option? |
|`error-handling-correctness-only`| don't care 🤷 |`return await promise;`| 🟡 Okay to use, but the above options would be preferable. |
51
+
|`never`|`return promise;`|`return promise;` <br/> (⚠️ This behavior may be harmful ⚠️) | ❌ No. This option is deprecated. |
52
+
29
53
### `in-try-catch`
30
54
31
-
In cases where returning an unawaited promise would cause unexpected error-handling control flow, the rule enforces that `await`must be used.
32
-
Otherwise, the rule enforces that `await` must _not_be used.
55
+
In error-handling contexts, the rule enforces that returned promises must be awaited.
56
+
In ordinary contexts, the rule enforces that returned promises _must not_be awaited.
33
57
34
-
Listing the error-handling cases exhaustively:
58
+
This is a good option if you prefer the shorter `return promise` form for stylistic reasons, wherever it's safe to use.
35
59
36
-
- if you `return` a promise within a `try`, then it must be `await`ed, since it will always be followed by a `catch` or `finally`.
37
-
- if you `return` a promise within a `catch`, and there is _no_`finally`, then it must _not_ be `await`ed.
38
-
- if you `return` a promise within a `catch`, and there _is_ a `finally`, then it _must_ be `await`ed.
39
-
- if you `return` a promise within a `finally`, then it must not be `await`ed.
40
-
- if you `return` a promise between a `using` or `await using` declaration and the end of its scope, it must be `await`ed, since it behaves equivalently to code wrapped in a `try` block.
60
+
Examples of code with `in-try-catch`:
41
61
42
62
<Tabs>
43
63
<TabItemvalue="❌ Incorrect">
@@ -169,7 +189,9 @@ async function validInTryCatch7() {
169
189
170
190
### `always`
171
191
172
-
Requires that all returned promises are `await`ed.
192
+
Requires that all returned promises be awaited.
193
+
194
+
This is a good option if you like the consistency of simply always awaiting promises, or prefer not having to consider the distinction between error-handling contexts and ordinary contexts.
173
195
174
196
Examples of code with `always`:
175
197
@@ -214,9 +236,49 @@ async function validAlways3() {
214
236
</TabItem>
215
237
</Tabs>
216
238
239
+
### `error-handling-correctness-only`
240
+
241
+
In error-handling contexts, the rule enforces that returned promises must be awaited.
242
+
In ordinary contexts, the rule does not enforce any particular behavior around whether returned promises are awaited.
243
+
244
+
This is a good option if you only want to benefit from rule's ability to catch control flow bugs in error-handling contexts, but don't want to enforce a particular style otherwise.
245
+
246
+
:::info
247
+
We recommend you configure either `in-try-catch` or `always` instead of this option.
248
+
While the choice of whether to await promises outside of error-handling contexts is mostly stylistic, it's generally best to be consistent.
249
+
:::
250
+
251
+
Examples of additional correct code with `error-handling-correctness-only`:
252
+
253
+
<Tabs>
254
+
<TabItemvalue="✅ Correct">
255
+
256
+
```ts option='"error-handling-correctness-only"'
257
+
asyncfunction asyncFunction():Promise<void> {
258
+
if (Math.random() <0.5) {
259
+
returnawaitPromise.resolve();
260
+
} else {
261
+
returnPromise.resolve();
262
+
}
263
+
}
264
+
```
265
+
266
+
</TabItem>
267
+
</Tabs>
268
+
217
269
### `never`
218
270
219
-
Disallows all `await`ing any returned promises.
271
+
Disallows awaiting any returned promises.
272
+
273
+
:::warning
274
+
275
+
This option is deprecated and will be removed in a future major version of typescript-eslint.
276
+
277
+
The `never` option introduces undesirable behavior in error-handling contexts.
278
+
If you prefer to minimize returning awaited promises, consider instead using `in-try-catch` instead, which also generally bans returning awaited promises, but only where it is _safe_ not to await a promise.
279
+
280
+
See more details at [typescript-eslint#9433](https://github.com/typescript-eslint/typescript-eslint/issues/9433).
0 commit comments