Skip to content

Instantly share code, notes, and snippets.

@drumnickydrum
Created February 6, 2023 22:36
Show Gist options
  • Save drumnickydrum/e5e19f979a1c176e0265865ba1c4f133 to your computer and use it in GitHub Desktop.
Save drumnickydrum/e5e19f979a1c176e0265865ba1c4f133 to your computer and use it in GitHub Desktop.
[RxJS: Observable Operator Pipeline] Create an observer with an operator pipeline #rxjs #observable #angular

Angular Docs: Rx Library | Operators

import { of } from 'rxjs';
import { filter, map } from 'rxjs/operators';

const squareOdd = of(1, 2, 3, 4, 5)
  .pipe(
    filter(n => n % 2 !== 0),
    map(n => n * n)
  );

// Subscribe to get values
squareOdd.subscribe(x => console.log(x));

Common operators

RxJS provides many operators, but only a handful are used frequently. For a list of operators and usage samples, visit the RxJS API Documentation.

Note that, for Angular applications, we prefer combining operators with pipes, rather than chaining. Chaining is used in many RxJS examples.

AREA OPERATORS
Creation from, fromEvent, of
Combination combineLatest, concat, merge, startWith , withLatestFrom, zip
Filtering debounceTime, distinctUntilChanged, filter, take, takeUntil
Transformation bufferTime, concatMap, map, mergeMap, scan, switchMap
Utility tap
Multicasting share
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment