]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/no-return-await.md
b1c60f092ae4b8694750f215c4e83ffa6ebc3c1c
[pve-eslint.git] / eslint / docs / rules / no-return-await.md
1 # Disallows unnecessary `return await` (no-return-await)
2
3 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.
4
5 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.
6
7 ## Rule Details
8
9 This rule aims to prevent a likely common performance hazard due to a lack of understanding of the semantics of `async function`.
10
11 Examples of **incorrect** code for this rule:
12
13 ```js
14 async function foo() {
15 return await bar();
16 }
17 ```
18
19 Examples of **correct** code for this rule:
20
21 ```js
22 async function foo() {
23 return bar();
24 }
25
26 async function foo() {
27 await bar();
28 return;
29 }
30
31 async function foo() {
32 const x = await bar();
33 return x;
34 }
35
36 async function foo() {
37 try {
38 return await bar();
39 } catch (error) {}
40 }
41 ```
42
43 In the last example the `await` is necessary to be able to catch errors thrown from `bar()`.
44
45 ## When Not To Use It
46
47 There are a few reasons you might want to turn this rule off:
48
49 - If you want to use `await` to denote a value that is a thenable
50 - If you do not want the performance benefit of avoiding `return await`
51 - If you want the functions to show up in stack traces (useful for debugging purposes)
52
53 ## Further Reading
54
55 [`async function` on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)
56
57 [`await vs return vs return await` by Jake Archibald](https://jakearchibald.com/2017/await-vs-return-vs-return-await/)