Angular — RxJS
What is RxJS?
Reactive Extensions Library for JavaScript — library for composing asynchronous code.
Creating Observables
import { Observable, of, from, interval, Subject } from 'rxjs';
// of
const numbers$ = of(1, 2, 3);
// from
const array$ = from([1, 2, 3]);
// interval
const timer$ = interval(1000);
// Subject
const subject$ = new Subject();
Transforming Operators
import { map, filter, reduce, switchMap, mergeMap, concatMap } from 'rxjs/operators';
// map
numbers$.pipe(map(x => x * 2));
// filter
numbers$.pipe(filter(x => x > 1));
// reduce
numbers$.pipe(reduce((acc, val) => acc + val, 0));
// switchMap (cancels previous)
searchControl.valueChanges.pipe(
switchMap(term => this.http.get(`/api/search?q=${term}`))
);
// mergeMap (runs in parallel)
ids$.pipe(
mergeMap(id => this.http.get(`/api/items/${id}`))
);
Combining Operators
import { merge, combineLatest, forkJoin } from 'rxjs';
// merge
merge(source1$, source2$);
// combineLatest
combineLatest([source1$, source2$]);
// forkJoin (waits for all to complete)
forkJoin([http1$, http2$, http3$]);
Error Handling
import { catchError, retry } from 'rxjs/operators';
import { of } from 'rxjs';
this.http.get('/api/data').pipe(
retry(3),
catchError(err => {
console.error(err);
return of([]);
})
);
Mini Practice
- Create observables
- Use transformation operators
- Combine multiple streams
- Handle errors
Up Next
Continue with Signals — Angular signals in depth.
Related Topics
Frequently Asked Questions about RxJS
What is RxJS in Angular?
RxJS is a fundamental concept in Angular. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn RxJS?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn RxJS.
Why is RxJS important in Angular?
RxJS is essential for Angular development. Understanding this concept will help you write better code and solve real-world problems more effectively.