Reactive programming
Реактивное программирование
Reactive programming describes how results depend on changing values or events. When an input changes, the program propagates that change through the declared dependencies instead of requiring each caller to update the result by hand.
Also known as: RxJS, реактивность
What it means
A shopping cart total depends on quantities, prices, and discounts. In an imperative implementation, every handler that changes one of those inputs needs to trigger a recalculation. In a reactive implementation, you declare the dependency once and let the reactive mechanism update the total when needed. The same idea lets you turn a sequence of keystrokes or incoming messages into a sequence of results.
How it works
Not all reactive code uses streams. A signal represents a current value, and a signal-based system can track which computations depend on it. Stream-based libraries such as RxJS treat changes as values delivered over time. An RxJS Observable can emit zero or more values and may terminate with completion or an error. A source such as DOM input events can stay active until you unsubscribe.
Operators let you build new streams from existing ones: map transforms values, and filter selects them. combineLatest waits until every input has emitted at least once, then emits a combination of their latest values whenever any input emits. A subscription connects a consumer to the pipeline. Observable does not mean asynchronous: a source can emit values during the call to subscribe itself.
Example: search input with RxJS
import { fromEvent, map, debounceTime, distinctUntilChanged } from 'rxjs';
const input = document.querySelector('#search');
const queries = fromEvent(input, 'input').pipe(
map(event => event.target.value.trim()),
debounceTime(300),
distinctUntilChanged()
);
const subscription = queries.subscribe(query => console.log(query));
// When the view is removed:
// subscription.unsubscribe();
Instead of logging on every keystroke, the pipeline waits for a 300 ms pause and skips consecutive duplicate queries. Clearing the field can still emit an empty string, which a real search interface could use to clear its results. This example stops at logging; it does not make an HTTP request.
When to use it
Reactive pipelines are useful when inputs interact, timing matters, or values keep arriving. Examples include validating a form against several fields, updating a live dashboard, and processing telemetry. For a single HTTP call with one result, a Promise and async/await are often simpler. RxJS is a tool for writing reactive code, not another name for the approach.
Watch the lifetime of long-running subscriptions: a subscription that outlives its view can keep references alive and continue doing work. Also, debouncing is not demand-based backpressure. It drops intermediate inputs but does not let a slow consumer tell the producer how much data it can accept.
In interviews
A common question is how to stop an old search response from replacing a newer one. In RxJS, switchMap unsubscribes from the previous inner observable when a new query arrives, so later emissions from that old observable no longer reach the output. Unsubscribing does not necessarily abort the network request itself: that depends on whether the source supports cancellation.