Basically I have an async function that fetches data from the pokeAPI. I go through the data and extract what I need then I want to store that data in a variable for future use. My problem is that once I come out of the function it seems that all the data is undefined however while inside the function the data clearly shows that it is being stored like I want it to be. I've exhausted all possible ideas I can think of for fixing or understanding the problem and am at my wits end here.
const getAllPokemon = async () => {
try {
const arr = [];
const response = await fetch("https://pokeapi.co/api/v2/pokemon?limit=5");
const data = await response.json();
const pokeResults = data.results;
for (let pokemon of pokeResults) {
arr.push(getPokemonData(pokemon));
}
return arr;
} catch (e) {
return "Unable to find pokemon";
}
};
const getPokemonData = async (pokemon) => {
let url = pokemon.url;
const response = await fetch(url);
const data = await response.json();
let pokeObj = new Object();
pokeObj.name = data.forms[0].name;
pokeObj.id = data.id;
return pokeObj;
};
const pokemon = getAllPokemon();
console.log(pokemon);