My app have to connect to PokeApi to obtain 12 random pokemons,now it’s doing this by randomly selecting 12 pokemon IDs and calling the API 12 times in the form:
fetch(`https://pokeapi.co/api/v2/pokemon/${id}`)
I wonder if there’s some way to chain those IDs in the url and recieve 12 objects in one JSON.
My full fetching code:
export async function fetchPokemons() {
let pokemonIDs = [];
let pokemons = [];
for (let i = 0; i < 12; i++) {
pokemonIDs.push(getRandomPokemonID());
}
const promises = pokemonIDs.map(async (id) => {
const promise = await fetch(`https://pokeapi.co/api/v2/pokemon/${id}`);
return promise.json();
});
pokemons = await Promise.all(promises);
console.log(pokemons);
pokemons = pokemons.map((pokemon) => ({
id: pokemon.id,
pokeName: pokemon.name,
image: pokemon.sprites.other["official-artwork"].front_default,
reactID: uuidv4(),
}));
return pokemons;
}