I wrote a function that gets an observable list of pokemon from the pokeapi.co API. If I want to use this function (getPokemons()), I need to subscribe to it. However the subscribe response returns an empty list, because in my function I do another API call using the subscribe function.
The internet clarified that subscribing within a http request is a common mistake, and that you need to use forkMap, flatMap, switchMap, mergeMap or whatever. Unfortunately, I do not understand how I can apply one of those functions to my case.
getPokemons(pageNumber: number) : Observable<Pokemon[]> {
var apiUrl = `${this.apiRoot}?&limit=${this.itemCount}&offset=${pageNumber * this.itemCount - (20)}`;
return this.httpClient.get<any>(`${apiUrl}`).pipe(
map(res => {
var results: Pokemon[] = [];
for (let pokemon of res.results) {
this.getPokemonByName(pokemon.name).subscribe(data => {
results.push(data);
},
err => {
console.error(err.error || 'API error');
});
}
return results;
})
);
}
getPokemonByName(name: string) : Observable<Pokemon> {
return this.httpClient.get<Pokemon>(`${this.apiRoot}/${name}`).pipe(
map(res => {
return new Pokemon(res);
})
);
}
In my scenario the original http.get() responds with a list that only has the name of the pokemon. I need to do another API call to get the details of a specific pokemon. Because getPokemonByName().subscribe() is async, the array 'results' stays empty.
I hope you understand my issue, and you can help me figure out how to work this out.