ptortyr45
ptortyr45

Reputation: 147

How to get data from the server through an interval?

I use angular6, i try get json from server through 5 seconds interval.

JSFIDDLE

Its my service:

getExample(): Observable<any> {
    console.log('getExample');
    return Observable
      .timer(0, 5000)
      .flatMap(() => this.httpClient.get('https://jsonplaceholder.typicode.com/posts'))
      .map((r) => {
        return r;
      });
  }

Please help fix this code.

PS: more simple service worked without problem

Upvotes: 1

Views: 140

Answers (1)

Explosion Pills
Explosion Pills

Reputation: 191749

RxJS 6 has updated to use pipeable operators. Also, timer imports from a different path. You should import timer (or interval in this case) directly and use .pipe with the operators that you need. Finally, use mergeMap instead of flatMap.

import { interval } from 'rxjs';
import { mergeMap, map } from 'rxjs/operators';

return interval(5000).pipe(
  mergeMap(() => this.httpClient.get('https://jsonplaceholder.typicode.com/posts')),
  map((r) => {
    console.log(r);
    return r;
  })
);

Upvotes: 1

Related Questions