Most JavaScript code is a list of instructions that run once, top to bottom. But real apps are full of things that happen over time: user clicks, keystrokes, API responses, timers. RxJS is a library for working with these streams of events, using a concept called Observables.
An Observable is like a promise that can deliver many values over time instead of just one. Here is a simple example — reacting to a search input:
import { fromEvent } from 'rxjs';
import { debounceTime, map, distinctUntilChanged } from 'rxjs/operators';
const searchInput = document.querySelector('#search');
fromEvent(searchInput, 'input')
.pipe(
map((event) => event.target.value),
debounceTime(300),
distinctUntilChanged()
)
.subscribe((query) => {
console.log('Searching for:', query);
});
This code waits until the user stops typing for 300 milliseconds, ignores repeated values, and only then runs the search. Without RxJS, you would write the same logic with timers, flags, and cleanup code — and it would be easy to get wrong.
The power of RxJS comes from its operators. There are over a hundred of them: filter to keep only some values, switchMap to cancel an old request when a new one starts, combineLatest to merge two streams, and catchError to handle failures gracefully. You chain them together like a pipeline, and each step transforms the stream.
RxJS has a learning curve — the vocabulary is new and the mental model takes time. But you do not need all of it at once. Start with one stream, like a search box or a button click, and add operators as you need them. Angular uses RxJS heavily, so Angular developers will meet it sooner or later.
RxJS is for developers building apps with lots of async events — live search, real-time dashboards, chat, or anything where values arrive over time and need to be combined, filtered, or cancelled cleanly.
Top comments (0)