Sébastien Lucas / Dev wiki
  • Blog Wiki
  • Story book
  • Developers comics
  • Angular
    • Angular CLI + Angular.json
    • ngTemplateOutlet
    • Angular Schematic
    • Interceptor
    • Micro frontend + Mono repo
    • Build a library module
    • Error handling
    • Virtual and infinite scroll
    • Angular i18n
    • Debug Angular
    • Angular LifeCycle Hook
    • Nested forms in Angular
    • Angular Recipes
    • Master component in Angular
    • Perfomance optimization
    • Service Workers + PWA
    • Mobile
    • Electron app / Desktop app
    • Unit test & Angular
      • Unit test recipes (Jasmine / Karma / Angular)
      • Testing services with httpMock
    • Communication between components
    • Angular snippet library
    • Release and version management
    • Angular tutorial selection
    • UI components libraries
    • Angular libraries
    • Angular Tutorials
    • NGRX
      • Angular NGRX / Tips and code examples
      • Angular new Redux (alternative to ngrx)
      • NGRX unit test
      • Angular ngrx / Basics
    • Angular 2/Angular 1 differences
  • Graphql
  • Three.js
  • Ag grid
  • Open source BIM and 3D
  • Javascript
    • Null vs undefined
    • Html API
    • Await API
    • Debug memory leaks
    • Offline and PWA
    • Javascript tutorials
    • Javascript recipes
    • Bluebird missing docs
    • Alternative to lodash with ES6
    • ES6 syntax the best parts
    • Vanilla JS
  • RXJS
    • Docs
    • Recipes
    • Mock API and sandbox
    • Observables rxjs recipes
    • Combination operators
  • NODE.js
    • Environment variables
    • Fix CORS
    • Pagination requests in API
    • API tests
    • Node.js security
    • Learn node.js
    • Best libraries for node.js
    • Mongoose recipe
    • Debug node.js
  • Gatsby / React
    • Hooks
    • React concepts
    • Gatsby internationalisation
  • Ghost blog
  • Services for developers
    • SaaS images services
    • Airtable API examples
  • MISC
    • JIRA debugging
    • Wordpress plugins
    • Interview Sébastien Lucas
    • English expression
    • True recipes
    • Science podcast
  • AI
    • Machine learning open source
    • Tensor flow
    • Machine learning
    • Code examples
    • Courses and tutorials
    • Datasets
    • The Future of AI
    • Learn algo and data structures
  • Typescript
    • Generic types
    • Typescript recipes
    • Advanced types
      • Conditional types
      • Type guards
    • d.ts files
  • Docker
    • Starting with docker
    • Dockerise simple node app
    • Docker by aymen el amri
  • Mongodb
    • Pattern and data modeling
  • Devops
    • Heroku
    • Scaleway
    • Github template
    • Gitlab CI
    • http2
    • nginx
    • zsh
    • CI Continuous integration
    • DNS
    • Devops resources
    • Gcloud useful commands
    • Authenticate Gcloud
    • Documentation generators
    • Firebase database
  • Developers ressources
    • Online platform coding
      • Online courses
      • Coding games
      • Coding test platforms
      • Links to check
    • Good developers blogs
    • Nice open source project / github
  • Tooling
    • The chrome urls
    • Linux Mac tips
    • Webstorm configuration
    • Develop in Windows
    • Mac debug hardware
    • Mac Setup for a developer
    • Chrome extension
    • Develop toolbox
  • HTML / CSS
    • Tailwind
    • Css grid
    • ☘️Nice styles & generators
    • Favicon
    • Flexbox grid
    • Flexbox layout
    • PUG templates tips
    • Html and css references
    • Css snippets
    • SASS-CSS libraries
    • New things in scss
    • SASS best practices
    • Style lint
  • Tests
    • Cypress
      • Learn cypress and more tests
      • Cypress commands
      • Cypress plugins
      • Page object && app actions
      • Flaky tests
    • Mobile test
    • BDD + Cucumber
    • Puppeteer
    • Type of tests + Ressources
    • Jasmine test
    • Mock, fake, stub in unit tests
    • E2e tests with protactor
    • Mocha
  • REVIT & AEC tools
  • Git
    • Git commits
    • Git tips
    • Git hooks
    • Set up a mono repo
  • Design Pattern
    • Functional Programming
  • Job board / Remote jobs
  • SVG
  • JSON
  • Github
    • Forking a github repo
  • NPM
    • Private NPM packages
    • Publish to NPM with np
  • Yarn
    • Yarn evolution / 2, 3...
    • Yarn Linking
Powered by GitBook
On this page
  • Unsubscribing to observable in Angular
  • Create a timer with observable
  • [Observable] Observable.combineLatest
  • [Subject] Behavior subject
  • [Operator] map
  • [Operator] flatmap
  • [Operator] do
  • [Operator] catch
  • [Operator] take
  • [Filter] filter
  • [Operator] withLatestFrom
  • [Operator] exhaustMap
  • [Function] pipe method
  • Return an observable when another observable emit a value

Was this helpful?

  1. RXJS

Observables rxjs recipes

PreviousMock API and sandboxNextCombination operators

Last updated 4 years ago

Was this helpful?

Unsubscribing to observable in Angular

Create a timer with observable

Create a timer that will emit a value every 5000ms.

const timer = Rx.Observable.interval(5000);

[Observable] Observable.combineLatest

  • When to use? : You have 2 queries or more. You need to do something depending on of the results of those queries. For example, you need to wait for page params and get the connected user information...

CombineLatest emits an item whenever any of the source Observables emits an item (so long as each of the source Observables has emitted at least one item)

So you have 2 or more async observables, the Observable.combineLatest is called after each emitted their first value, and give the opportunity to a function to remit a item with the value of the last emitted item

[Subject] Behavior subject

A variable that is changed using next() and "watched" using subscribe

  • When to use? : When we need to keep an updatable value (for example the connected user) and get all components updated when this value change

[Operator] map

import 'rxjs/add/operator/map';

Allow to change the value of each emitted items before it is sent to function subscribing to it.

[Operator] flatmap

[Operator] do

import 'rxjs/add/operator/do';

  • When to use? : To perform an action like logging that do not transform the observable result

[Operator] catch

import 'rxjs/add/operator/catch';

  • When to use? : to catch error, return an observable with the error information

[Operator] take

import 'rxjs/add/operator/take';

  • When to use? : When you want to limit the number of returned elements. For example take only the 5 first elements of a stream of observable.

[Filter] filter

  • When to use? : Select the condition to receive a given observable, for example you want to make an API call only if the parameters are available

[Operator] withLatestFrom

import 'rxjs/add/operator/withLatestFrom';

  • When to use? Combine 2 sources with the latest emitted version for the second source. withLatestFrom is generally followed by a map where the 2 sources results .map([source1, source2) => { //handle 2 sources } can be manipulated

[Operator] exhaustMap

  • When to use?: Map values to inner observable and stop emitting value until inner observable map finish (=exhaust)

    this.actions$.ofType(Auth.LOGIN)
      .map((action: Auth.Login) => action.payload)
      .withLatestFrom(this.geolocationService.refreshLocation())
      // auth and geo are mapped to inner observable (sub-observable)
      // if new values are emitted but  inner observable are not completed
      // they won't be mapped again
      .exhaustMap(([auth, geo]) => this.authService.login(auth, geo)
          .map(loginAuth => new Auth.LoginSuccess({ auth: loginAuth }))
          .catch(error => of(new Auth.LoginFailure(error)))
      );

[Function] pipe method

Pipe method is available from Rxjs 5.5

Return an observable when another observable emit a value

this.closeEditing$ 
.pipe(switchMap(() => this.lines$))
.subscribe(lines => {

A good comparison of behaviorSubject ReplaySubject and asyncSubject

When to use? : After API query when we generally want to convert to JSON, control with libraries like ...

Mind that in JS the name is not flatmap ! Look at flatmap.html

https://blog.codecentric.de/en/2018/01/different-ways-unsubscribing-rxjs-observables-angular/
https://medium.com/@benlesh/rxjs-dont-unsubscribe-6753ed4fda87
https://medium.com/angular-in-depth/the-best-way-to-unsubscribe-rxjs-observable-in-the-angular-applications-d8f9aa42f6a0
js bin example of 2 sources (2 intervals emitters) with the second slower
official doc
official doc on subjects
https://medium.com/@luukgruijs/understanding-rxjs-behaviorsubject-replaysubject-and-asyncsubject-8cc061f1cfc0
TypedJson
official doc on map operator
Difference between map & flapmap
flatmap official doc
Learn RXJS doc on do
Learn RXJS doc on catch
Learn RXJS doc on take
Learn RXJS on filter
Learn RXJS doc on withlatestfrom
Learn RXJS doc on exhaustMap
https://blog.hackages.io/rxjs-5-5-piping-all-the-things-9d469d1b3f44
How to Understand RxJS Operators by Eating a Pizza: zip, forkJoin, & combineLatest Explained with ExamplesfreeCodeCamp.org
Logo