RxJS Operators
Learn how RxJS operators process Observable streams and how operators can be combined using the pipe() method.
What is an RxJS Operator?
An RxJS Operator is a function that works with an Observable stream and returns another Observable representing the processed stream.
Operators can be used for transformation, filtering, combination, timing, error handling and other stream-processing requirements.
RxJS operators are commonly combined inside the pipe() method.
RxJS Operator Flow
Why Do We Use Operators?
Transform Data
Convert emitted values into the format required by the application.
Filter Data
Allow only required values to continue through the stream.
Combine Streams
Work with values from multiple Observable sources.
Handle Errors
Build stream-processing logic for error scenarios.
Using pipe()
The pipe() method is used to connect multiple RxJS operators together.
Operators are executed as part of the Observable processing pipeline.
const result$ = source$.pipe(
operator1(),
operator2(),
operator3()
);
Operator Categories
Transformation
Changes or converts emitted values.
Filtering
Controls which values are allowed to continue.
Combination
Works with multiple Observable streams.
Utility
Supports timing, side effects and stream control.
Common RxJS Operators
| Operator | Category | Purpose |
|---|---|---|
map()
|
Transformation | Transforms each emitted value |
filter()
|
Filtering | Keeps values matching a condition |
tap()
|
Utility | Performs a side effect without changing the value |
take()
|
Filtering / Control | Takes a specified number of emissions |
catchError()
|
Error Handling | Handles an error in the stream |
debounceTime()
|
Timing | Delays emission until the source remains quiet |
Simple Operator Example
const numbers$ = of(
10,
20,
30
);
const result$ = numbers$.pipe(
map(value => value * 2)
);
result$.subscribe({
next: value => {
console.log(value);
}
});
Output
20
40
60
Chaining Operators
More than one operator can be placed inside a single pipe().
This creates a processing pipeline where the result of one operator is passed to the next operator.
const numbers$ = of(
1,
2,
3,
4,
5
);
const result$ = numbers$.pipe(
map(value => value * 10),
filter(value => value > 20)
);
Operators and Observable Immutability
RxJS operators generally create a new Observable representing the processed stream rather than directly changing the original Observable definition.
This makes stream-processing pipelines easier to compose and reason about.
Transformation Operators
map()
Converts each value into another value.
scan()
Accumulates state as values are emitted.
buffer()
Collects emitted values into grouped outputs.
format
Transformation logic can prepare data for application use.
Detailed transformation operators will be covered in the next topic, Transformation and Filtering.
Filtering Operators
filter()
Keeps values satisfying a condition.
take()
Takes a fixed number of emitted values.
skip()
Ignores a specified number of initial values.
distinct
Helps control repeated values in a stream.
Utility Operators
tap()
Useful for logging and debugging side effects.
debounceTime()
Controls rapid successive emissions using time.
catchError()
Provides error-handling behavior for a stream.
finalize()
Runs logic when an Observable terminates.
CIIT Training Institute Example
Suppose CIIT Training Institute receives course enquiry information through an Observable.
Operators can be placed in a pipeline to prepare the data before the dashboard receives the final result.
const enquiries$ = of(
'Java',
'.NET',
'Python'
);
const result$ = enquiries$.pipe(
map(course =>
course + ' enquiry'
),
tap(enquiry => {
console.log(
'Processing:',
enquiry
);
})
);
result$.subscribe({
next: enquiry => {
console.log(
'Dashboard:',
enquiry
);
}
});
CIIT Training Institute
Processing: Java enquiry
Processing: .NET enquiry
Processing: Python enquiry
Dashboard receives processed enquiry data
Choosing an Operator
| Requirement | Suitable Operator | Purpose |
|---|---|---|
| Change a value |
map()
|
Transform each emission |
| Keep matching values |
filter()
|
Apply a condition |
| Inspect values |
tap()
|
Perform a side effect |
| Limit emissions |
take()
|
Take a fixed number of values |
| Handle errors |
catchError()
|
Handle stream failures |
| Delay rapid events |
debounceTime()
|
Control frequent emissions |
Building a Good Operator Pipeline
Identify Source
Understand what values the Observable emits.
Define Requirement
Decide whether data must be transformed or filtered.
Select Operators
Choose operators that directly match the requirement.
Build Pipeline
Combine the selected operators inside pipe().
Important Points
Operators are used to process values emitted by an Observable.
Multiple operators can be combined through the pipe() method.
Transformation, filtering, combination, timing and error-handling operators solve different problems.
Select operators based on the actual business or application requirement.
RxJS Operator Best Practices
- Choose an operator based on the exact stream-processing requirement.
- Keep operator chains readable.
- Avoid adding operators that do not contribute to the required result.
- Use tap() mainly for side effects such as logging or debugging.
- Handle important errors with suitable RxJS error-handling strategies.
- Separate complex stream-processing logic into meaningful services or reusable methods where appropriate.
Summary
RxJS operators are functions used to process Observable streams.
Operators can transform, filter, combine, control and handle errors in data streams.
The pipe() method is used to compose operators into a processing pipeline. In the next topic, we will study Transformation and Filtering in detail.