Observed retry / retryWhen with flatmap

I have the following code to get the id, and then the data associated with the id.

process(): Observable<any> {
    let id: number;
    return this.getId().flatMap(
        (response: number) => {
                id = response;
                return this.getData(id)
        }
    ).flatMap(
        (data: any) => {
            return Observable.of(data);
        }
    );
}

getData(id: number): Observable<any> {
    // retry if response.status is not complete
    return this.service.getData(id).map(response => return response.data); 
}

I would like to add retry logic to the getData method until response.status is complete. I tried adding a repeat / retryWhen after this.getData (id), but no luck. Any suggestions please?

+4
source share
2 answers

retry retryWhen , , , . , , , .

, , :

getData(id) {
    return Observable.interval(100)
        // make 5 attempts to get the data
        .take(5)
        .concatMap(() => this.service.getData(id))
        .filter(response => response.status === "complete")
        .take(1)
        .map(response => response.data)
}

getData() , , "" , - 5 ( ).

Update:

, "" retry, :

getData(id) {
    return this.service.getData(id)
        .concatMap(response => response.status === "complete" ?
            Observable.of(response) :
            Observable.throw("Bad status"))
        .map(response => response.data)
        .retry(5);
}

, 6 (1 + 5 ), , , , catch. , "" (, ) , - , retryWhen.

+4

, .

(Observable ...).retry(2).map(...).subscribe(...)

HTTP-, , , 404, .

0

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


All Articles