Skip to content

Commit 00716a3

Browse files
mikedidomizionzakasmdjermanovic
authored
docs: upfront recommend against using the no-return-await rule (#19727)
* docs: upfront recommend against using the no-return-await rule * Fix unordered list style for lint * Remove paragraph. Add horizontal line to break up note * Remove mention of extra microtask as no longer correct * Update no-return-await.md * Update docs/src/rules/no-return-await.md Co-authored-by: Milos Djermanovic <[email protected]> * Update docs/src/rules/no-return-await.md --------- Co-authored-by: Nicholas C. Zakas <[email protected]> Co-authored-by: Milos Djermanovic <[email protected]>
1 parent b9080cf commit 00716a3

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

docs/src/rules/no-return-await.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ further_reading:
77
- https://jakearchibald.com/2017/await-vs-return-vs-return-await/
88
---
99

10-
The original intent of this rule was to discourage the use of `return await`, to avoid an extra microtask. However, due to the fact that JavaScript now handles native `Promise`s differently, there is no longer an extra microtask. More technical information can be found in [this V8 blog entry](https://v8.dev/blog/fast-async).
10+
It is NOT recommended to use the `no-return-await` rule anymore because:
1111

12-
Using `return await` inside an `async function` keeps the current function in the call stack until the Promise that is being awaited has resolved, at the cost of an extra microtask before resolving the outer Promise. `return await` can also be used in a try/catch statement to catch errors from another function that returns a Promise.
12+
* `return await` on a promise will not result in an extra microtask.
13+
* `return await` yields a better stack trace for debugging.
1314

14-
You can avoid the extra microtask by not awaiting the return value, with the trade off of the function no longer being a part of the stack trace if an error is thrown asynchronously from the Promise being returned. This can make debugging more difficult.
15+
Historical context: When promises were first introduced, calling `return await` introduced an additional microtask, one for the `await` and one for the return value of the async function. Each extra microtask delays the computation of a result and so this rule was added to help avoid this performance trap. Later, [ECMA-262 changed the way](https://github.com/tc39/ecma262/pull/1250) `return await` worked so it would create a single microtask, which means this rule is no longer necessary.
1516

1617
## Rule Details
1718

18-
This rule aims to prevent a likely common performance hazard due to a lack of understanding of the semantics of `async function`.
19+
This rule warns on any usage of `return await` except in `try` blocks.
1920

2021
Examples of **incorrect** code for this rule:
2122

@@ -65,8 +66,4 @@ async function foo4() {
6566

6667
## When Not To Use It
6768

68-
There are a few reasons you might want to turn this rule off:
69-
70-
* If you want to use `await` to denote a value that is a thenable
71-
* If you do not want the performance benefit of avoiding `return await`
72-
* If you want the functions to show up in stack traces (useful for debugging purposes)
69+
You should not use this rule. There is no reason to avoid `return await`.

0 commit comments

Comments
 (0)