ConfigsApr 11, 2026·2 min read

RxJS — Reactive Programming Library for JavaScript

RxJS is a reactive extensions library for JavaScript using Observables for composing asynchronous and event-based programs. 100+ operators for transforming, filtering, and combining streams. The foundation of Angular and many complex UIs.

TL;DR
RxJS models async data as observable streams you compose with declarative operators.
§01

What it is

RxJS is the reactive extensions library for JavaScript. It implements the Observable pattern, letting you treat click events, HTTP responses, WebSocket messages, and timers as composable data streams. With 100+ operators for transforming, filtering, combining, and error-handling, RxJS is the standard approach to complex async logic in frontend and backend JavaScript.

RxJS is the reactive foundation of Angular. It also powers many standalone applications where event ordering, debouncing, retries, or concurrent request management would be painful with raw Promises or callbacks.

§02

Why it saves time or tokens

Without RxJS, managing concurrent HTTP calls, user input debouncing, and WebSocket reconnection requires scattered state variables and nested callbacks. RxJS replaces that mess with a pipeline of operators that reads top-to-bottom. When asking an AI assistant to generate async logic, specifying RxJS operators produces shorter, more predictable output than describing the same behavior imperatively.

§03

How to use

  1. Install the library: npm install rxjs
  2. Import the operators you need: import { fromEvent, debounceTime, switchMap } from 'rxjs'
  3. Build an observable pipeline and subscribe to it
§04

Example

import { fromEvent } from 'rxjs';
import { debounceTime, map, switchMap } from 'rxjs/operators';

const search$ = fromEvent(input, 'input').pipe(
  debounceTime(300),
  map(e => (e.target as HTMLInputElement).value),
  switchMap(query => fetch('/api/search?q=' + query).then(r => r.json()))
);

search$.subscribe(results => renderResults(results));

This pipeline debounces user keystrokes by 300ms, extracts the input value, cancels any in-flight request when a new keystroke arrives (switchMap), and renders results.

OperatorPurposeCommon Use
debounceTimeWait N ms after last emissionSearch input
switchMapCancel previous inner observableHTTP requests
combineLatestEmit when any source emitsForm validation
retryResubscribe on errorNetwork resilience
takeUntilComplete when notifier emitsComponent teardown
§05

Related on TokRepo

§06

Common pitfalls

  • Forgetting to unsubscribe causes memory leaks; use takeUntil, take, or Angular's async pipe
  • Nesting subscribes inside subscribes defeats the purpose; flatten with switchMap, mergeMap, or concatMap
  • Choosing the wrong flattening operator (mergeMap vs switchMap vs concatMap) leads to race conditions or dropped requests

Frequently Asked Questions

What is the difference between switchMap and mergeMap?+

switchMap cancels the previous inner observable when a new outer value arrives. mergeMap keeps all inner observables running concurrently. Use switchMap for search-as-you-type where only the latest result matters. Use mergeMap when every emission must complete independently, like logging events to a server.

Is RxJS only for Angular?+

No. RxJS is a standalone library that works in any JavaScript or TypeScript environment including React, Vue, Node.js, and plain browser scripts. Angular bundles RxJS and uses it heavily for HTTP, routing, and forms, but the library itself has no Angular dependency.

How do I avoid memory leaks with RxJS?+

Always unsubscribe from long-lived observables when the consumer is destroyed. In Angular, use the async pipe or takeUntil with a destroy subject. In React, unsubscribe in a useEffect cleanup function. For one-shot observables like HTTP requests, manual unsubscription is usually unnecessary.

Can AI assistants generate RxJS code effectively?+

Yes. Because RxJS operators have well-defined, composable semantics, language models produce accurate pipelines when you name the operators you want. Specifying 'use switchMap for the HTTP call' or 'debounce by 300ms' gives the model enough constraint to generate correct reactive code.

What version of RxJS should I use in 2026?+

RxJS 7.x is the current stable branch. It reduced bundle size, improved TypeScript typings, and deprecated several legacy patterns from RxJS 6. Most new projects should start with RxJS 7. Check the official migration guide if upgrading from version 6 to understand renamed operators and removed APIs.

Citations (3)
  • RxJS GitHub— RxJS provides 100+ operators for composing async programs
  • Angular Docs— RxJS is the reactive foundation of Angular
  • RxJS Docs— switchMap cancels previous inner observable on new emission

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets