Created
July 15, 2020 11:48
-
-
Save NyaGarcia/e36e28ff0884e307c76276913736268b to your computer and use it in GitHub Desktop.
Nested subscribe to inner Observables
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
import { map } from 'rxjs/operators'; | |
import { ajax } from 'rxjs/ajax'; | |
import { of } from 'rxjs'; | |
const pokemonId$ = of(1, 5, 6); | |
function getPokemonName(id: number) { | |
return ajax.getJSON(`https://pokeapi.co/api/v2/pokemon/${id}`).pipe( | |
map(({ name }) => name)); | |
} | |
pokemonId$ | |
.pipe( | |
map(id => getPokemonName(id)), | |
) | |
.subscribe(innerObservable$ => innerObservable$.subscribe(console.log)); | |
// Output: bulbasaur, charizard, charmeleon |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment