I am working on this Pokemon API app where I want to model object for each individual Pokemon. My function looks like this:
const createPokemonObject = () => {
fetchPokemonData().then(data => {
const { results } = data
results.map(async (pokemon) => {
const { url } = pokemon
const data = await fetch(url)
const response = data.json()
response.then(data => {
const { name, id, sprites: {other: {dream_world: {front_default}}}, types, weight, stats } = data
const pokemonObject = {
id: id,
name: name,
image: front_default,
weight: weight,
types: types,
stats: stats
}
console.log(pokemonObject)
})
})
})
}
fetchPokemonData()
function is returning promise, which is asynchronous code, and then I am using async function again inside of array to get definite data and model the object for each Pokemon. Object looks like this:
My question is how can I rewrite this createPokemonObject()
function so that is returns a promise on which I can later chain then()
function with all pokemonObject
objects inside, with whom I could make some UI elements later?