const getRecipes = async (flasks) => {
let newList = []
let recipes = flasks[0].recipes;
await Promise.all(recipes.map(async (recipe) => {
const response = await axios.get(`https://us.api.blizzard.com/data/wow/recipe/${recipe.id}?namespace=static-us&locale=en_US&access_token=${api_token}
`)
newList.push(response.data)
}))
// flasks[0].recipes.forEach(recipe => {axios.get(`https://us.api.blizzard.com/data/wow/recipe/${recipe.id}?namespace=static-us&locale=en_US&access_token=${api_token}
// `).then(response =>
// newList.push(response.data)
// )})
console.log(new Date())
console.log('logging newList')
console.log(newList)
return newList;
// setpotions_recipes(newPotions);
}
useEffect(() => {
axios.get(`https://us.api.blizzard.com/data/wow/connected-realm/104/auctions?namespace=dynamic-us&locale=en_US&access_token=${api_token}`).then(
response => {
const auctionList = response.data.auctions;
setAuctions(auctionList);
console.log('fetched auctions');
}
)
main();
}, [])
const main = async () => {
getRecipes(flasks).then( response => {
console.log('response:')
console.log(response)
getIngredientsList(response)
})
}
const getIngredientsList = (recipe_type) => {
let newList = new Map(ingredientsMap); //ingredientsMap is a state that holds information, this function tries to update ingredients (an array version of ingredientsMap)
console.log('In getIngredientsList');
console.log(recipe_type);
console.log(recipe_type.length);
recipe_type.forEach(item => {
console.log('inside recipe_type forEAch')
item.reagents.forEach(reagent => {
newList.set(`${reagent.reagent.name}`, {id: reagent.reagent.id, min: Infinity})
}
)
})
console.log(new Date())
setIngredientsMap(newList);
let newArray = Array.from(ingredientsMap, ([name, {id, min}]) => ({name, id, min}))
console.log(`logging ingredients from ${recipe_type}`)
console.log(newArray);
setIngredients(newArray);
}
Essentially I am trying to grab a set of items from an api, and then parse those items afterwards. I'm new to asynchronous functions so what I tried is to make getRecipes asynchronous and once that returns its list, call getIngredientsList on that response.
However, getIngredientsList logs recipe_type to be of length 0. I believe this is because my asynchronous calls are not lining up correctly, but don't exactly know how to fix it.