| | |

RxJS 18 🟣 Cheatsheet

⏱️ Estimated read: 3 min | |

This cheatsheet provides a quick reference for core RxJS concepts, operator categories, and common patterns.


1. Creation Operators

Use these to start your streams.

OperatorUsageEmits
of(1, 2, 3)Static values1, 2, 3, then completes
from([1, 2, 3])Array, Promise, or IterableEach item individually
interval(1000)Timer0, 1, 2… every second
timer(1000)Delay0 after 1s, then completes
fromEvent(el, 'click')DOM eventThe event object
new Subject()Manual streamWhatever you next()

2. Flattening Operators (The “Concurrency” Models)

The most critical choice in any RxJS pipeline.

OperatorBehaviorBest Used For
mergeMapRuns all inner streams in parallelIndependent tasks (e.g., uploading files)
switchMapCancels previous inner streamTypeahead, routing, search
concatMapRuns inner streams sequentiallyQueued tasks (e.g., database writes)
exhaustMapIgnores new input until inner finishesLogin buttons, form submits

3. Transformation & Filtering

Modify the data flow.

OperatorPurpose
map(v => v * 2)Transforms values
filter(v => v > 0)Discards values that fail the check
take(n)Completes after N values
debounceTime(300)Ignores bursts, emits last after silence
distinctUntilChangedOnly emits if value is different from last
tap(v => console.log(v))Perform side effects (debugging, logging)

4. Subjects (State Containers)

Subject TypeBehavior
SubjectStandard: new subscribers get nothing.
BehaviorSubject(init)Requires init value: replays current value to new subs.
ReplaySubject(n)Replays last N values to new subs.
AsyncSubjectEmits only the last value on completion.

5. Error Handling & Lifecycle

  • catchError((err, caught) => ...): Intercept errors, return a new observable to recover.
  • retry(n): Resubscribes to source N times on error.
  • finalize(() => ...): Runs on complete OR error OR unsubscribe. Perfect for cleanup.
  • takeUntil(notifier$): The standard for “destroying” a stream when a component unmounts.

6. Combining Streams

OperatorBehavior
combineLatest([a$, b$])Emits when any input emits (requires all to have emitted once)
merge(a$, b$)Emits whenever any input emits
forkJoin([a$, b$])Emits only when all complete (last values only)
concat(a$, b$)Emits a$ completely, then b$

7. Memory Leak Checklist (The “Don’t Leak” Rule)

  1. Every Subscription needs a Teardown:
    • Use takeUntil(destroy$) for long-lived components.
    • Use finalize() for closing sockets/resources.
  2. Avoid Subscription Nesting:
    • Never subscribe inside a subscribe. Use flattening operators (mergeMap, etc.) instead.
  3. Check your Operators:
    • If using shareReplay, ensure refCount: true is set if you want the stream to clean up when subscribers leave.

8. Quick Pattern: Typeahead Search

const search$ = input$.pipe(
  debounceTime(300),
  distinctUntilChanged(),
  switchMap(term => fetchApi(term).pipe(
    catchError(() => of([])) // Prevent stream death
  ))
);

9. Quick Pattern: The Bridge (Angular/Signal)

// RxJS to Signal
const mySignal = toSignal(myObservable$, { initialValue: 0 });

// Signal to RxJS
const myObservable$ = toObservable(mySignal).pipe(
  filter(val => val > 10)
);

10. The Scheduler Hierarchy (Task Timing)

  1. queueScheduler: Synchronous trampoline (no delay).
  2. asapScheduler: Microtask (after current sync block, before paint).
  3. animationFrameScheduler: Browser frame (before paint).
  4. asyncScheduler: Timer (macrotask, after paint).

Was this helpful?


Stop using slow, ad-bloated tool sites! 🤮

🔎 Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)

⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free

🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!