im trying to make an array empty and use map to change the array fatch but when a do console.log on array it return's a pending promise
const arrayPokemons = Array(5)
.fill()
.map((item, index) =>
fetch(`https://pokeapi.co/api/v2/pokemon/${index + 1}`)
.then((res) => res.json())
.then((data) => data.name)
);
console.log(arrayPokemons);
You just need to resolve the promises. The code you provided maps each element of the array to a Promise, so you now have five Promises. To resolve them all -- wait for all of them to resolve -- and access the results, you can use Promise.all.
The revised snippet below does that, and works as you intended.
const promises = Array(5)
.fill()
.map((item, index) =>
fetch(`https://pokeapi.co/api/v2/pokemon/${index + 1}`)
.then((res) => res.json())
.then((data) => data.name)
);
Promise.all(promises).then((arrayPokemons) => {
console.log(arrayPokemons)
})