Skip to content

Latest commit

 

History

History
46 lines (30 loc) · 1.48 KB

no-nested-subscribe.md

File metadata and controls

46 lines (30 loc) · 1.48 KB

Disallow calling subscribe within a subscribe callback (rxjs-x/no-nested-subscribe)

💼 This rule is enabled in the following configs: ✅ recommended, 🔒 strict.

💭 This rule requires type information.

This rule effects failures if subscribe is called within a subscribe handler.

Rule details

Examples of incorrect code for this rule:

import { of, timer } from "rxjs";

of(42, 54).subscribe((value) => {
  timer(1e3).subscribe(() => console.log(value));
});

Examples of correct code for this rule:

import { of, timer } from "rxjs";
import { map, mergeMap } from "rxjs/operators";

of(42, 54).pipe(
  mergeMap((value) => timer(1e3).pipe(map(() => value)))
).subscribe((value) => console.log(value));

When Not To Use It

If you need nested subscriptions and are aware of the potential issues, then you might not need this rule. However, you should typically prefer to use higher-order mapping operators like mergeMap, switchMap, or concatMap to handle nested observables.

Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting.

Resources