I'm having problems with making an RxJS5 observable stream behave in a way that I want it to.
The stream is supposed to send an HTTP request to a website using axios, and if the response is an HTTP error (which axios coerces to a JavaScript error), the observable sequence should wait 10 milliseconds and then try resending the request (for some reason the website that I'm sending the request to doesn't like it when you retry sending the request immediately and keeps throwing errors, but mostly behaves fine with a 10 ms delay).
Rx.Observable
.fromPromise(axios('http://example.com/12345'))
.map(x => new Promise((resolve, reject) => {
setTimeout(() => {
resolve(x)
}, 2000)
}))
.debounceTime(2000)
.do(console.log)
.retry(10)
.subscribe(console.log, console.error)
I have an example on Codepen with a few changes, to make it more apparent, how the stream works: http://codepen.io/leakyabstractions/pen/pNmvyZ?editors=0010
I tried using .delay()
, .debounceTime()
, .timer()
, .timeInterval()
and .timeout()
in the place of the .map()
operator, but nothing (including .map()
) works. What am i doing wrong?
So basically what you are looking for is a "retry after 10ms, but only 10 times"? (this is what your
retry(10)
suggests. I think a sophisticated solution would includeretryWhen
here:Here is the code-pen: http://codepen.io/anon/pen/bBydwZ?editors=0010
Please also note, that I set the delay to 100ms instead of 10ms just so it shows a little cleaner in the console.