Skip to main content
Practice Problems

Common RxJS operators in Angular

RxJS Operators in Angular

RxJS operators transform, filter, and combine Observable streams. Knowing the key operators is essential for Angular development.


Transformation Operators

map — Transform each value

typescript
this.http.get<ApiResponse>('/api/users').pipe( map(response => response.data) // Extract data from response );

switchMap — Map to new Observable (cancels previous)

typescript
// Search with auto-cancel of previous request this.searchControl.valueChanges.pipe( debounceTime(300), switchMap(query => this.http.get(\`/api/search?q=\${query}\`)) );

mergeMap — Map to new Observable (parallel)

typescript
// Process items in parallel from(userIds).pipe( mergeMap(id => this.userService.getUser(id)) );

concatMap — Map to new Observable (sequential)

typescript
// Process one at a time, in order from(items).pipe( concatMap(item => this.apiService.save(item)) );

Filtering Operators

filter

typescript
this.route.paramMap.pipe( map(params => params.get('id')), filter((id): id is string => id !== null) );

debounceTime — Wait for pause

typescript
this.searchInput.valueChanges.pipe( debounceTime(300), // Wait 300ms after last keystroke distinctUntilChanged(), // Only emit if value changed switchMap(query => this.search(query)) );

distinctUntilChanged

typescript
this.store.select('count').pipe( distinctUntilChanged() // Only emit when value actually changes );

take / takeUntil

typescript
// Take first N values this.http.get('/api/config').pipe(take(1)); // Unsubscribe pattern private destroy$ = new Subject<void>(); ngOnInit() { this.someObservable$.pipe( takeUntil(this.destroy$) ).subscribe(/* ... */); } ngOnDestroy() { this.destroy$.next(); this.destroy$.complete(); }

Combination Operators

combineLatest

typescript
// Combine multiple streams — emits when ANY source emits combineLatest([ this.userService.getUser(id), this.orderService.getOrders(id), this.settingsService.getSettings() ]).pipe( map(([user, orders, settings]) => ({ user, orders, settings })) );

forkJoin

typescript
// Wait for ALL to complete — like Promise.all forkJoin({ users: this.http.get<User[]>('/api/users'), products: this.http.get<Product[]>('/api/products'), config: this.http.get<Config>('/api/config'), }).subscribe(({ users, products, config }) => { // All three completed });

Error Handling Operators

catchError / retry

typescript
this.http.get('/api/data').pipe( retry(3), // Retry up to 3 times catchError(error => { this.errorService.handle(error); return of([]); // Return fallback value }) );

Quick Reference

OperatorPurposeWhen to Use
mapTransform valuesAlways
switchMapMap + cancel previousSearch, route changes
mergeMapMap + run parallelParallel API calls
concatMapMap + run sequentialOrdered saves
filterSkip unwanted valuesConditional processing
debounceTimeWait for pauseSearch input
distinctUntilChangedSkip duplicatesForm changes
takeUntilAuto-unsubscribeComponent cleanup
combineLatestCombine latest valuesMultiple data sources
forkJoinWait for all to completeParallel requests
catchErrorHandle errorsError recovery

Important:

Master switchMap (cancels previous — perfect for search), takeUntil (clean unsubscribe pattern), and combineLatest/forkJoin (parallel data loading). Use debounceTime + distinctUntilChanged + switchMap for type-ahead search. Always handle errors with catchError.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.

Finished reading?
Practice Problems