Skip to content

Instantly share code, notes, and snippets.

@NyaGarcia
Last active March 23, 2020 20:15
Show Gist options
  • Select an option

  • Save NyaGarcia/70a6de58c090891a05413a8b2cb78f76 to your computer and use it in GitHub Desktop.

Select an option

Save NyaGarcia/70a6de58c090891a05413a8b2cb78f76 to your computer and use it in GitHub Desktop.
Using the share operator to turn cold Observables hot
pokemon$ = http.get(/* make an http request here*/).pipe(share());
/*The pokemon$ Observable is now hot, we won't have multiple http requests*/
pokemon$
.pipe(
flatMap(pokemon => pokemon),
filter(({ type }) => type === "Fire")
)
.subscribe(pokemon => {
// Do something with pokemon
});
pokemon$.pipe(switchMap(pokemon => getStats(pokemon))).subscribe(stats => {
// Do something with stats
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment