]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # Disallows unnecessary `return await` (no-return-await) |
2 | ||
3 | Inside an `async function`, `return await` is seldom useful. Since the return value of an `async function` is always wrapped in `Promise.resolve`, `return await` doesn’t actually do anything except add extra time before the overarching Promise resolves or rejects. The only valid exception is if `return await` is used in a try/catch statement to catch errors from another Promise-based function. | |
4 | ||
5 | ## Rule Details | |
6 | ||
7 | This rule aims to prevent a likely common performance hazard due to a lack of understanding of the semantics of `async function`. | |
8 | ||
9 | Examples of **incorrect** code for this rule: | |
10 | ||
11 | ```js | |
12 | async function foo() { | |
13 | return await bar(); | |
14 | } | |
15 | ``` | |
16 | ||
17 | Examples of **correct** code for this rule: | |
18 | ||
19 | ```js | |
20 | async function foo() { | |
21 | return bar(); | |
22 | } | |
23 | ||
24 | async function foo() { | |
25 | await bar(); | |
26 | return; | |
27 | } | |
28 | ||
29 | async function foo() { | |
30 | const x = await bar(); | |
31 | return x; | |
32 | } | |
33 | ||
34 | async function foo() { | |
35 | try { | |
36 | return await bar(); | |
37 | } catch (error) {} | |
38 | } | |
39 | ``` | |
40 | ||
41 | In the last example the `await` is necessary to be able to catch errors thrown from `bar()`. | |
42 | ||
43 | ## When Not To Use It | |
44 | ||
45 | If you want to use `await` to denote a value that is a thenable, even when it is not necessary; or if you do not want the performance benefit of avoiding `return await`, you can turn off this rule. | |
46 | ||
47 | ## Further Reading | |
48 | ||
49 | [`async function` on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) | |
50 | ||
51 | [`await vs return vs return await` by Jake Archibald](https://jakearchibald.com/2017/await-vs-return-vs-return-await/) |