rxjs
How to get current value of RxJS Subject or Observable?
A Subject or Observable doesn’t have a current value. When a value is emitted, it is passed to subscribers and the Observable is done with it. If you want to have a current value, use BehaviorSubject which is designed for exactly that purpose. BehaviorSubject keeps the last emitted value and emits it immediately to new … Read more
Hot and Cold observables: are there ‘hot’ and ‘cold’ operators?
I am coming back a few months later to my original question and wanted to share the gained knowledge in the meanwhile. I will use the following code as an explanation support (jsfiddle): var ta_count = document.getElementById(‘ta_count’); var ta_result = document.getElementById(‘ta_result’); var threshold = 3; function emits ( who, who_ ) {return function ( x … Read more
Avoiding nesting subscriptions in Angular 2+?
Avoiding nested subscriptions depends on the nature of the observables and how they depend on each other: Co-dependent observables When an observable (this.grants.get()) depends on the notification from another observable (this.user), you could use any of the RxJS higher order mapping operators switchMap, mergeMap, concatMap and exhaustMap. Each has their own purpose. You could find … Read more
Angular HTTP GET with TypeScript error http.get(…).map is not a function in [null]
I think that you need to import this: import ‘rxjs/add/operator/map’ Or more generally this if you want to have more methods for observables. WARNING: This will import all 50+ operators and add them to your application, thus affecting your bundle size and load times. import ‘rxjs/Rx’; See this issue for more details.
Is it necessary to unsubscribe from observables created by Http methods?
So the answer is no, you don’t. Ng2 will clean it up itself. The Http service source, from Angular’s Http XHR backend source: Notice how it runs the complete() after getting the result. This means it actually unsubscribes on completion. So you don’t need to do it yourself. Here is a test to validate: fetchFilms() … Read more
What is the difference between Subject and BehaviorSubject?
A BehaviorSubject holds one value. When it is subscribed it emits the value immediately. A Subject doesn’t hold a value. Subject example (with RxJS 5 API): const subject = new Rx.Subject(); subject.next(1); subject.subscribe(x => console.log(x)); Console output will be empty BehaviorSubject example: const subject = new Rx.BehaviorSubject(0); subject.next(1); subject.subscribe(x => console.log(x)); Console output: 1 In … Read more
What is the difference between Promises and Observables?
What is the difference between Promises and Observables?
What is the correct way to share the result of an Angular Http network call in RxJs 5?
EDIT: as of 2021, the proper way is to use the shareReplay operator natively proposed by RxJs. See more details in below answers. Cache the data and if available cached, return this otherwise make the HTTP request. import {Injectable} from ‘@angular/core’; import {Http, Headers} from ‘@angular/http’; import {Observable} from ‘rxjs/Observable’; import ‘rxjs/add/observable/of’; //proper way to … Read more
Angular/RxJS When should I unsubscribe from `Subscription`
TL;DR For this question there are two kinds of Observables – finite value and infinite value. http Observables produce finite (1) values and something like a DOM event listener Observable produces infinite values. If you manually call subscribe (not using async pipe), then unsubscribe from infinite Observables. Don’t worry about finite ones, RxJs will take … Read more