I have a problem with handling an array where I need to emit one value every x seconds, call a function with that value, and retry if the function fails. The type of values in the array is not important.
This is what I have implemented so far:
Rx.Observable
.interval(500)
.take(arr.length)
.map(idx => arr[idx])
.flatMap(dt => randomFunc(dt))
.catch(e => console.log(e))
.retry(5)
.subscribe();
function randomFunc(dt) {
return Rx.Observable.create(observer => {
if (dt === 'random') {
return observer.error(`error`);
} else {
return observer.next();
}
});
}
However, there are two issues that I am facing:
1: When randomFunc
returns an error, the entire chain seems to restart instead of just retrying the failed function.
2: The catch
block does not log any errors, despite appearing to retry on error.
To address the first issue, I attempted using switchMap
instead of flatMap
:
Rx.Observable
.interval(500)
.take(arr.length)
.map(idx => arr[idx])
.switchMap(dt => randomFunc(dt)
.catch(e => console.log(e))
.retry(5)
)
.subscribe();
While this appeared to retry only the failed functions, the error logging functionality still did not work. Also, I am unsure if switchMap
is appropriate in this context as I am new to Rx programming.
Any assistance or guidance would be greatly appreciated. Thank you!