javascript - Observable retry / retryWhen with flatmap -


i have following code id , data related 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 not complete     return this.service.getdata(id).map(response => return response.data);  } 

i add retry logic on getdata method until response.status not complete. tried adding retry/retrywhen after this.getdata(id) no luck. suggestions please?

retry , retrywhen suitable error handling, meaning source observable has produce error these operators make effect. guess in case want retry not on error, on status don't like.

probably simplest way achieve make service calls on interval , take first response status want:

getdata(id) {     return observable.interval(100)         // make 5 attempts data         .take(5)         .concatmap(() => this.service.getdata(id))         .filter(response => response.status === "complete")         .take(1)         .map(response => response.data) } 

now getdata() returns observable either emits single value corresponds response "complete" status , completes, or completes without emitting value after 5 unsuccessful attempts data desired status (the answer updated in response comment).

update:

it is, of course, possible turn responses "bad" statuses errors , use retry functionality, proposed:

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); } 

if things go wrong, code try fetch data @ 6 times (1 initial + 5 retries) , produce error have handle, example catch operator. 1 drawback way not distinguish "real" errors (e.g. network failures) errors produced out of bad statuses, handled retrywhen.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -