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.
Review-first install path
This asset needs a review step. The copied prompt tells the agent to dry-run, show the writes, then proceed only after confirmation.
npx -y tokrepo@latest install 25e2a014-35b8-11f1-9bc6-00163e2b0d79 --target codexDry-run first, confirm the writes, then run this command.
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.
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.
How to use
- Install the library:
npm install rxjs - Import the operators you need:
import { fromEvent, debounceTime, switchMap } from 'rxjs' - Build an observable pipeline and subscribe to it
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.
| Operator | Purpose | Common Use |
|---|---|---|
| debounceTime | Wait N ms after last emission | Search input |
| switchMap | Cancel previous inner observable | HTTP requests |
| combineLatest | Emit when any source emits | Form validation |
| retry | Resubscribe on error | Network resilience |
| takeUntil | Complete when notifier emits | Component teardown |
Related on TokRepo
- AI tools for coding — more developer libraries and frameworks curated on TokRepo
- Automation tools — tools that help automate repetitive development tasks
Common pitfalls
- Forgetting to unsubscribe causes memory leaks; use
takeUntil,take, or Angular'sasyncpipe - Nesting subscribes inside subscribes defeats the purpose; flatten with
switchMap,mergeMap, orconcatMap - Choosing the wrong flattening operator (mergeMap vs switchMap vs concatMap) leads to race conditions or dropped requests
Frequently Asked Questions
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.
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.
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.
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.
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
Related on TokRepo
Discussion
Related Assets
RxSwift — Reactive Programming for Swift
The Swift implementation of Reactive Extensions (Rx) providing observable sequences and operators for composing asynchronous and event-based programs on Apple platforms.
RxJava — Reactive Extensions for the JVM
RxJava is a library for composing asynchronous and event-based programs using observable sequences on the JVM, enabling clean handling of complex data flows and concurrency.
RxDB — Reactive Local-First Database for JavaScript
A local-first, reactive NoSQL database for JavaScript applications with real-time replication, offline support, and flexible storage backends.
MobX — Simple Scalable State Management for JavaScript
MobX is a transparent reactive state management library that makes state management simple and scalable by applying functional reactive programming principles to JavaScript and TypeScript applications.