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 77<!-- end auto-generated rule header -->
88
99This 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 ` ).
1013
1114## Rule details
1215
1316Examples of ** incorrect** code for this rule:
1417
1518``` ts
1619import { 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+ });
1826```
1927
2028Examples of ** correct** code for this rule:
2129
2230``` ts
2331import { 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 ));
2537```
2638
2739## Further reading
2840
2941- [ 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