Skip to content

Instantly share code, notes, and snippets.

@zimejin
Created May 30, 2019 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zimejin/4e22ad835e7971a867185dbb72265b72 to your computer and use it in GitHub Desktop.
Save zimejin/4e22ad835e7971a867185dbb72265b72 to your computer and use it in GitHub Desktop.
RxJS snippets
//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