Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions docs/rules/no-async-subscribe.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,36 @@
<!-- end auto-generated rule header -->

This rule effects failures if async functions are passed to `subscribe`.
Developers are encouraged to avoid race conditions
by instead using RxJS operators which can handle both Promises and Observables
(e.g. `concatMap`, `switchMap`, `mergeMap`, `exhaustMap`).

## Rule details

Examples of **incorrect** code for this rule:

```ts
import { of } from "rxjs";
of(42).subscribe(async () => console.log(value));

of(42).subscribe(async value => {
const data1 = await fetch(`https://api.some.com/things/${value}`);
const data2 = await fetch(`https://api.some.com/things/${data1.id}`);
console.log(data2);
});
```

Examples of **correct** code for this rule:

```ts
import { of } from "rxjs";
of(42).subscribe(() => console.log(value));

of(42).pipe(
switchMap(value => fetch(`http://api.some.com/things/${value}`)),
switchMap(data1 => fetch(`http://api.some.com/things/${data1.id}`)),
).subscribe(data2 => console.log(data2));
```

## Further reading

- [Why does this rule exist?](https://stackoverflow.com/q/71559135)
- [Higher-order Observables](https://rxjs.dev/guide/higher-order-observables)