Shaping the Flow: Basic RxJS Operators

Unlock the potential of reactive programming. Master map, filter, and switchMap to build robust, event-driven applications.

Pipeline ProgressStep 1 of 8
🌊
0 EXP

Welcome. Imagine data isn't static, but a river flowing over time. In RxJS, this river is called an Observable.

// Data flows like a stream...
const stream$ = new Observable();

RxJS Fundamentals

Reactive Extensions for JavaScript (RxJS) is a library for reactive programming using Observables, to make it easier to compose asynchronous or callback-based code. Think of an Observable as a stream of events that can happen over time (like mouse clicks, or data arriving from a server).

import { Observable } from 'rxjs';

const stream$ = new Observable(subscriber => {
  subscriber.next(1);
  subscriber.next(2);
});

Reactive Check

What do we call the consumer who listens to the data emitted by an Observable?

Advanced Holo-Simulations

0 EXP

Apply your knowledge in these advanced simulations. All our content is free!


Achievements

🏄
Stream Surfer

Successfully filter a data stream in the simulation.

🔧
Pipe Plumber

Construct a valid RxJS pipe chain without errors.

🧙‍♂️
Transformation Wizard

Correctly identify the usage of switchMap vs map.

Mission: Construct a Pipeline

Write a code snippet that takes an Observable source, uses .pipe(), applies filter to select specific items, maps them using map, and finally subscribes to it.

Architect Feedback:

> Awaiting input...

Challenge: Order the Stream

Drag the code blocks to represent the correct chronological flow of data in an RxJS stream.

.subscribe(val => console.log(val))
source$
.pipe(map(x => x * 2))

Challenge: Complete the Syntax

Fill in the missing RxJS keywords to make this code functional.

import {} from 'rxjs';
const src$ = of(1, 2, 3).(map(x => x*2));
src$.(val => console.log(val));

Consult The Architect

Reactive Holo-Net

Peer Code Review

Submit your "Pipeline" project for feedback from other RxJS developers.

Mastering the Flow: A Deep Dive into RxJS Operators

Reactive programming with RxJS introduces a paradigm shift: instead of writing code that executes once, we write code that reacts to streams of data over time. At the heart of this paradigm are **Operators**. Operators are the tools that allow us to shape, transform, filter, and combine these streams into useful applications.

Why Do We Need Operators?

An raw `Observable` is just a source of events. Without operators, you would have to write complex logic inside every `.subscribe()` block to handle logic like buffering, debouncing, or transforming API responses. Operators allow us to perform this logic **declaratively** inside the `.pipe()`.

The "Big Three" Categories

1. Transformation Operators (e.g., `map`)

Just like `Array.prototype.map`, the RxJS `map` operator takes a value emitted by the source, runs it through a function, and emits the result.

source$.pipe(
  map(user => user.name)
).subscribe(name => console.log(name));

2. Filtering Operators (e.g., `filter`)

These act as gates. If the logic returns `false`, the value is discarded and never reaches the subscriber. This is essential for performance and logic control.

3. Combination/High-Order Operators (e.g., `switchMap`)

This is where RxJS shines. `switchMap` is a "Flattening Operator". It maps each value to an Observable, then flattens all of these inner Observables. Crucially, `switchMap` **cancels** the previous inner Observable if a new value arrives.

✔️ Use switchMap when...

You are handling user input (search) where old requests become obsolete immediately upon new input.

✔️ Use mergeMap when...

You want to process every request in parallel (e.g., saving multiple unrelated items) and don't want cancellation.

Pro Tip: Always think of your data as a stream. Ask yourself: "Do I want to change the data (`map`), stop the data (`filter`), or handle a new stream based on the data (`switchMap`)?"

RxJS Terminology & Glossary

Observable
A representation of any set of values over any amount of time. It is a lazy push collection of multiple values.
Observer
A collection of callbacks that knows how to listen to values delivered by the Observable.
Subscription
Represents the execution of an Observable. It is primarily useful for cancelling the execution (unsubscribing).
Operator
A pure function that takes an Observable as input and generates a new Observable as output (e.g., `map`, `filter`).
Pipe
A method used to chain operators together so that the output of one becomes the input of the next.
Marble Diagram
A visual representation of how operators change the stream of data over time.
switchMap
A flattening operator that projects each source value to an Observable which is merged in the output Observable, emitting values only from the most recently projected Observable.

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching Angular, RxJS, and building robust reactive applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on RxJS 7+ specifications and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!