portfolio / notes / rxjs-fundamentals.mdoc
Understand the stream, then the operator
A visual guide to RxJS fundamentals: Observables versus Promises, how to read a stream diagram, pipe() and subscribe(), and the first three operators: map, filter and tap.
- RxJS
- Angular

Stop memorizing operator names. Follow what happens to each value instead: once you can read one value through a pipe, every operator is just a rule about what it does to that value.
One result, or a stream of values
A Promise settles once: one fulfillment value or one rejection, at most. new Promise(executor) runs the executor immediately, and its .then() callbacks always run asynchronously. It has no built-in cancellation method.
An Observable delivers zero to many values, and may complete or error. A cold source starts a new execution for each subscription; a hot source can share a producer that is already running. Emissions can be synchronous or asynchronous.
unsubscribe() stops your subscription, not necessarily the source.
| Promise | Observable | |
|---|---|---|
| Values | One, or a rejection | Zero to many |
| Starts | The executor runs at once, in new Promise() | On subscribe (cold) or already running (hot) |
| Timing | Callbacks always asynchronous | Synchronous or asynchronous |
| Cancel | No built-in method | unsubscribe() stops your subscription |
Reading the diagram
Time moves from left to right. Each dot is a value; each line is a stream.
next(A): a value is emitted.|complete: the stream ends normally.Xerror: the stream ends with a failure.
No more values arrive after complete or error. These lines are not execution threads: they only show when values happen.
pipe() composes, subscribe() connects
const doubled$ = source$.pipe(map((n) => n * 2));
doubled$.subscribe(console.log);pipe() composes the operators into a new Observable without subscribing to the chain; a hot source may already be producing values on its own. subscribe() registers the consumer and, on a cold source, starts its execution for that subscription. With Angular's HttpClient, each subscription sends a request. Angular's async pipe can manage the subscription for you.
unsubscribe() ends your side, finalize() cleans up
There is a third way for a subscription to end, and it comes from the other side. unsubscribe() is not a notification of the Observable: nothing is emitted, and neither complete nor error runs. It is something the subscriber does to its subscription: stop listening, and let the source release what it was holding for it.
That release is the teardown: the function a source returns when it is created. It runs however the subscription ends: complete, error or unsubscribe. finalize() lets you hook into the same moment from a pipe.
const ticks$ = new Observable<number>((subscriber) => {
const id = setInterval(() => subscriber.next(Date.now()), 1000);
return () => clearInterval(id); // teardown
});
const sub = ticks$
.pipe(finalize(() => console.log('cleaned up')))
.subscribe(render);
sub.unsubscribe(); // the interval stops and 'cleaned up' is logged| How it ends | Who ends it | Observer callback | Teardown and finalize |
|---|---|---|---|
| complete | The source | complete() | Run |
| error | The source | error(err) | Run |
unsubscribe() | The subscriber | None | Run |
map changes the value, filter decides what passes
filter drops emissions; it does not emit true or false.
tap observes without transforming
Returning a value from tap does not change the emitted value. Use it to observe; avoid mutating objects inside it.
Putting it together
Follow one value and read the whole pipe:
source$
.pipe(
map((n) => n * 10),
filter((n) => n >= 20),
tap((n) => console.log(n)),
)
.subscribe(render);| Input | After map | Output |
|---|---|---|
| 1, 2, 3 | 10, 20, 30 | 20, 30 |
2 becomes 20. It passes the filter. tap logs 20, and the subscriber receives 20.
map transforms, filter selects, tap observes.
Check yourself
Pick an answer to see whether it is right, and why.
Sources: Observable and operators in the RxJS docs, and the HTTP client guide in Angular's docs.