Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I dont get rxjs 6 with angular 6 with interval, switchMap, and map

Tags:

I want to update my rxjs code to 6 got I don't get it.

Before I had the below that wouth poll for new data every 5 seconds:

import { Observable, interval } from 'rxjs'; import { switchMap, map } from 'rxjs/operators';  var result = interval(5000).switchMap(() => this._authHttp.get(url)).map(res => res.json().results); 

Now...of course, it's broken and the documentation leaves me nowhere to go.

How do I write the above to conform to rxjs 6?

Thanks

like image 794
Tampa Avatar asked May 06 '18 14:05

Tampa


People also ask

What is difference between map and switchMap RxJS?

map(function(value) { return value*10; }). subscribe(observer); SwitchMap - switchMap will subscribe to all the inner Observables inside the outer Observable but it does not merge the inner Observables. It instead switches to the latest Observable and passes that along to the chain.

How does switchMap work in RxJS?

RxJS switchMap() operator is a transformation operator that applies a project function on each source value of an Observable, which is later merged in the output Observable, and the value given is the most recent projected Observable.

How do you use RxJS interval?

RxJS interval() Creation OperatorRxJS interval() operator is a creation operator used to create an observable that emits a sequence of integers every time for the given time interval on a specified SchedulerLike. It emits incremented numbers periodically in time.

What is switchMap in angular RxJS?

Angular Tap. Angular MergeMap. The Angular SwitchMap maps each value from the source observable into an inner observable, subscribes to it, and then starts emitting the values from it. It creates a new inner observable for every value it receives from the Source.


1 Answers

The code should be something like the following. You need to use the pipe operator.

import { interval } from 'rxjs'; import { switchMap, map } from 'rxjs/operators';  const result = interval(5000).pipe( switchMap(() => this._authHttp.get(url)),     map(res => res.results) ) 
like image 148
siva636 Avatar answered Sep 22 '22 05:09

siva636