Rxjs Observed Interval and Angular2 HTTP: Waiting for a Response

I am trying to create a service that polls a log at a given interval (every 5 seconds). I need to make an HTTP GET call, but with slow connections and large logs it switchMapcancels the previous pending request. This way I never get the log as the request is canceled.

getLog(url:string):Observable<string> {
    return Observable
        .timer(0, 5000)
        .switchMap(() => this.get(url))
        .retryWhen(error => error.delay(5000))
        .map((res:Response) => res.text())
        .catch(e => {
            console.warn(e.toString());
            return Observable.from("");
        });
}

and this.get(url)- it's simple get(url) {return this.http.get(url)}.

I am trying to save the timer function, but not start another HTTP call until the previous call has been resolved, and so as not to cancel the waiting call.

+4
source share
1 answer

concatMap() switchMap().

, switchMap() . flatMap() ( mergeMap()) Observable .

, concatMap() , Observable , Observable. , , , .

+3

Source: https://habr.com/ru/post/1674405/


All Articles