1
votes

SHORT VERSION

how can I make the rxjs observable.pipe(repeat(10), retryWhen()) run together? I want my angular http.get to run 10 times, no matter if it fails or succeeds

LONG VERSION

so I have an angular 5 app that sends a request to a server to process many distinct services at the same time, and as each one of them complete, a json is filled until all services complete.

as soon as my app requests that, it starts http.getting the response json, that might (and probably) wont be complete the first few times it requests. Knowing that, I want the http.get observable to do the following:

  • Try getting the response a max number of times (10 times, for example)
  • If the json returns complete before that, end the observable
  • The server returns an error when no services finished yet, but as soon as the first service finishes, a json is returned successfully, so I want retry(10) and repeat(10) rxjs functions to run together (with the same index, so if it fails 2 times, the repeat should be in the 3rd try too)

this is what I have so far:

http.get(route).pipe(
    retryWhen(myRetry()), //myRetry() has no implementation yet
    repeat(10),
    takeWhile(json => !json.finished)
).subscribe(...);

but I believe this retries 10 times first, then repeats 10 times, which isnt what I want. Any suggestions on better structuring/changing the whole thing is welcome

1

1 Answers

0
votes

Just use repeat and ignore errors

http.get(route).pipe(
    catchError(() => EMPTY)
    repeat(10),
    takeWhile(json => !json.finished)
).subscribe(...);