Skip to content

Commit 477b0c7

Browse files
thw0rtedbradzacher
andauthored
docs(eslint-plugin): [no-misused-promises] improve examples (typescript-eslint#3898)
* Improve no-misused-promises docs Fixes typescript-eslint#3892 * formatting Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
1 parent ebb33ed commit 477b0c7

File tree

1 file changed

+49
-16
lines changed

1 file changed

+49
-16
lines changed

packages/eslint-plugin/docs/rules/no-misused-promises.md

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,25 @@ while (promise) {
2323
}
2424
```
2525

26+
Examples of **correct** code with `checksConditionals: true`:
27+
28+
```ts
29+
const promise = Promise.resolve('value');
30+
31+
// Always `await` the Promise in a conditional
32+
if (await promise) {
33+
// Do something
34+
}
35+
36+
const val = (await promise) ? 123 : 456;
37+
38+
while (await promise) {
39+
// Do something
40+
}
41+
```
42+
43+
---
44+
2645
Examples of **incorrect** code for this rule with `checksVoidReturn: true`:
2746

2847
```ts
@@ -37,37 +56,51 @@ new Promise(async (resolve, reject) => {
3756

3857
const eventEmitter = new EventEmitter();
3958
eventEmitter.on('some-event', async () => {
59+
synchronousCall();
4060
await doSomething();
61+
otherSynchronousCall();
4162
});
4263
```
4364

44-
Examples of **correct** code for this rule:
65+
Examples of **correct** code with `checksVoidReturn: true`:
4566

4667
```ts
47-
const promise = Promise.resolve('value');
48-
49-
if (await promise) {
50-
// Do something
51-
}
52-
53-
const val = (await promise) ? 123 : 456;
54-
55-
while (await promise) {
56-
// Do something
57-
}
58-
68+
// for-of puts `await` in outer context
5969
for (const value of [1, 2, 3]) {
6070
await doSomething(value);
6171
}
6272

73+
// If outer context is not `async`, handle error explicitly
74+
Promise.all(
75+
[1, 2, 3].map(async value => {
76+
await doSomething(value);
77+
}),
78+
).catch(handleError);
79+
80+
// Use an async IIFE wrapper
6381
new Promise((resolve, reject) => {
64-
// Do something
65-
resolve();
82+
// combine with `void` keyword to tell `no-floating-promises` rule to ignore unhandled rejection
83+
void (async () => {
84+
await doSomething();
85+
resolve();
86+
})();
6687
});
6788

89+
// Name the async wrapper to call it later
6890
const eventEmitter = new EventEmitter();
6991
eventEmitter.on('some-event', () => {
70-
doSomething();
92+
const handler = async () => {
93+
await doSomething();
94+
otherSynchronousCall();
95+
};
96+
97+
try {
98+
synchronousCall();
99+
} catch (err) {
100+
handleSpecificError(err);
101+
}
102+
103+
handler().catch(handleError);
71104
});
72105
```
73106

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy