Created
May 30, 2019 15:17
-
-
Save zimejin/4e22ad835e7971a867185dbb72265b72 to your computer and use it in GitHub Desktop.
RxJS snippets
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
//emit value every 1s | |
const source = interval(1000); | |
const example = source.pipe( | |
map(val => { | |
if (val > 5) { | |
//error will be picked up by retryWhen | |
throw val; | |
} | |
return val; | |
}), | |
retryWhen(errors => | |
errors.pipe( | |
//log error message | |
tap(val => console.log(`Value ${val} was too high!`)), | |
//restart in 6 seconds | |
delayWhen(val => timer(val * 1000)) | |
) | |
) | |
); | |
/* | |
output: | |
0 | |
1 | |
2 | |
3 | |
4 | |
5 | |
"Value 6 was too high!" | |
--Wait 6 seconds then repeat | |
*/ | |
const subscribe = example.subscribe(val => console.log(val)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment