Skip to content

Instantly share code, notes, and snippets.

@ben-barbier
Last active December 17, 2020 00:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ben-barbier/65cadda8571ac06a7fff8d6712678fd5 to your computer and use it in GitHub Desktop.
Save ben-barbier/65cadda8571ac06a7fff8d6712678fd5 to your computer and use it in GitHub Desktop.
⚡️RxJS 🚀, mergemap with retry and fallback
public launchCachePreloading() {
this.nomenclatureService.getUniverses().pipe(
flatMap(e => e),
mergeMap(universe => this.nomenclatureService.getFamilies(universe.id).pipe(
flatMap(e => e),
map(family => ({universeId: universe.id, familyId: family.id})),
)),
toArray(),
mergeMap(families => interval(1000).pipe(
filter(() => this.router.url === '/'),
take(families.length),
scan((acc) => acc + 1, -1),
map(idx => families[idx]),
mergeMap((e) => of(e).pipe(
mergeMap(() => this.nomenclatureService.getProducts(e.universeId, e.familyId)),
retry(2),
catchError(() => EMPTY),
)),
))
).subscribe(e => {
console.log('val: ' + JSON.stringify(e));
}, err => {
console.log('err: ' + err);
}, () => {
console.log('finish !!!');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment