Skip to content

Instantly share code, notes, and snippets.

@NyaGarcia
Last active July 11, 2020 15:58
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 NyaGarcia/1aed6a260b61b3fa2f2b3b285a64a102 to your computer and use it in GitHub Desktop.
Save NyaGarcia/1aed6a260b61b3fa2f2b3b285a64a102 to your computer and use it in GitHub Desktop.
const pokemonId$ = of(1, 5, 6);
function getPokemonName(id: number) {
return ajax.getJSON(`https://pokeapi.co/api/v2/pokemon/${id}`).pipe(
map(({ name }) => name),
delay(2000)
);
}
// concatAll subscribes to each inner Observable only after the previous one has completed: it will wait until each request is done, before making a new one
pokemonId$
.pipe(
map(id => getPokemonName(id)),
concatAll()
)
.subscribe(console.log);
// Output: (2s) bulbasaur, (2s) charmeleon, (2s) charizard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment