I'm trying to use PokeAPI with JS.
I can get Pokeman information successfully and display it, but not in the right order.
I used a forEach
loop and would like to wait until I add the first Pokeman and then add the second...
I've discovered await
and I tried to use it but I can't do it.
Can someone help me please?
Here is my JS code :
var url = 'https://pokeapi.co/api/v2/pokemon/';
fetch(url)
.then(function(response) {
return response.json();
})
.then(function(response) {
response.results.forEach(element => {
var url = 'https://pokeapi.co/api/v2/pokemon/' + element.name;
fetch(url)
.then(function(response) {
return response.json();
})
.then(function(response) {
addPokemon(response.name, response.sprites.front_default, response.types[0].type.name, response.height, response.weight);
})
});
})