File tree Expand file tree Collapse file tree 1 file changed +15
-2
lines changed Expand file tree Collapse file tree 1 file changed +15
-2
lines changed Original file line number Diff line number Diff line change 7
7
<!-- end auto-generated rule header -->
8
8
9
9
This rule effects failures if async functions are passed to ` subscribe ` .
10
+ Developers are encouraged to avoid race conditions
11
+ by instead using RxJS operators which can handle both Promises and Observables
12
+ (e.g. ` concatMap ` , ` switchMap ` , ` mergeMap ` , ` exhaustMap ` ).
10
13
11
14
## Rule details
12
15
13
16
Examples of ** incorrect** code for this rule:
14
17
15
18
``` ts
16
19
import { of } from " rxjs" ;
17
- of (42 ).subscribe (async () => console .log (value ));
20
+
21
+ of (42 ).subscribe (async value => {
22
+ const data1 = await fetch (` https://api.some.com/things/${value } ` );
23
+ const data2 = await fetch (` https://api.some.com/things/${data1 .id } ` );
24
+ console .log (data2 );
25
+ });
18
26
```
19
27
20
28
Examples of ** correct** code for this rule:
21
29
22
30
``` ts
23
31
import { of } from " rxjs" ;
24
- of (42 ).subscribe (() => console .log (value ));
32
+
33
+ of (42 ).pipe (
34
+ switchMap (value => fetch (` http://api.some.com/things/${value } ` )),
35
+ switchMap (data1 => fetch (` http://api.some.com/things/${data1 .id } ` )),
36
+ ).subscribe (data2 => console .log (data2 ));
25
37
```
26
38
27
39
## Further reading
28
40
29
41
- [ Why does this rule exist?] ( https://stackoverflow.com/q/71559135 )
42
+ - [ Higher-order Observables] ( https://rxjs.dev/guide/higher-order-observables )
You can’t perform that action at this time.
0 commit comments