2

Wonder why my promise is resolving but attempting to retry.

var getResultsStream = url => Rx.Observable.onErrorResumeNext( 
     Rx.Observable.defer( () => Rx.Observable
        .fromPromise( getSearchResults(url)
        .catch(error => Rx.Observable.of(`Error: ${error}`)) )
        .timeout(20000, new Error(`Timeout: ${url}`))
     )
     .do( e => console.log(`Retrying: ${url}`))
     .retry(3)
)

Is it a better way to retry a promise 3 times?

1 Answers1

4

Unless you have a special use for the defer and/or the onErrorResumeNext, you could throw that all out and simply use:

const request = url => Rx.Observable.of(url)
        .do(url => console.log("requesting: " + url))
        .switchMap(url => Rx.Observable.fromPromise(getSearchResults(url)))
        .timeout(20000, new Error(`Timeout: ${url}`))
        .retry(3);

request("http://foobar.com").subscribe(console.log, console.error);

function getSearchResults(url) {
  // simulating request-error
  throw new Error("Could not reach: " + url);
}
<script src="https://npmcdn.com/@reactivex/rxjs@5.0.0-beta.10/dist/global/Rx.umd.js"></script>
olsn
  • 16,644
  • 6
  • 59
  • 65
  • i added `onErrorResumeNext ` so it wouldn't break the stream if an error happened, in this case, how would i keep the stream alive if it failed 3 times? –  Jan 26 '17 at 15:44
  • 1
    Why would you want to do that? Nothing can happen on it any more - all retries are used - but also this is one of the basic principles of rxjs, that errors are handled correctly, this SO-entry is somewhat related: http://stackoverflow.com/questions/41827371/how-do-i-throw-an-error-on-a-behaviour-subject-and-continue-the-stream/41828984#41828984 – olsn Jan 26 '17 at 15:48
  • 1
    And last but not least: `onErrorResumeNext` will not keep the stream alive (see the docs here: https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/onerrorresumenext.md) – olsn Jan 26 '17 at 15:51
  • I'm fairly new to rxjs but, if i have 20 urls, one retries 3 times and failed, won't the stream error out and never complete? –  Jan 26 '17 at 16:03
  • also, isn't `onErrorResumeNext ` will ignore the error and move to the next step? Excuse my ignorance on this, but isn't that keeping the stream alive from erroring out? –  Jan 26 '17 at 16:08
  • 1
    but there is no next step - so it will just stop (which is totally fine, because for this stream there is really no need to keep it alive) - Streams that should NOT be kept alive: rest-calls, processing of data, basically anything that can throw an error - Streams that SHOULD be kept alive: data-providers, socket-connections, triggers, listeners for user-inputs, basically anythings that is just emitting data and won't throw an error – olsn Jan 26 '17 at 16:09
  • 1
    I'm an idiot, this is a stream within a much larger one, after this stream its a dozen or so other steps. I just wanted to make sure those are still ok –  Jan 26 '17 at 18:51
  • Okay - then depending on how this stream is implemented in the larger stream you might want to add a `catch` - but this is probably a separate question ;-) – olsn Jan 26 '17 at 20:15
  • NOTE: the RxJs team has removed the overload of `.timeout()` which takes an optional CustomError in 5.0 final. – Mark van Straten Jan 27 '17 at 08:24