Created
August 11, 2017 20:36
-
-
Save ccnokes/a79c3ee787d19309468f7e227b1b7732 to your computer and use it in GitHub Desktop.
Rx.Observable.retry example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function randomErrorObs() { | |
return Rx.Observable.create(obs => { | |
let n = 0; | |
if(Math.random() < 0.5) { | |
obs.next(++n); | |
} else { | |
console.warn('producing error'); | |
obs.error('bummer'); | |
} | |
}); | |
} | |
// retries 3 times, completes on the 4th failure | |
Rx.Observable.interval(300) | |
.switchMapTo(randomErrorObs()) | |
.retry(3) | |
.subscribe(console.log, console.error, () => console.log('completed')) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment