Z
Z
zlodiak2018-08-15 00:02:13
JavaScript
zlodiak, 2018-08-15 00:02:13

How to get data periodically?

I'm in an angular6 project trying to fetch data every 5 seconds. Wrote a service, but it does not work.
JSFIDDLE
Please help me fix the code:

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

This triggers a simpler service - in which there is no interval: JSFIDDLE

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolay Bolotnikov, 2019-03-26
@zlodiak

In RxJS 6 version, the pipe operator was added, in which the stream should be changed

import { Observable, timer } from 'rxjs';
import { flatMap } from 'rxjs/operators';

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question