Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

104
Vistas
Add value from API to an array in async function not working

Here is my code:

// get "foods" array from Firebase
const foods = querySnapshot.docs.map((doc) => ({ ...doc.data() })); 
  
// loop in "foods" array and recover food's price
const fetchPrices = async () => {
  for (const food of foods) {
    const response = await fetch(`${api}/${food.id}`);
    if (!response.ok) {
      // throw error
    }
    const result = await response.json()
    food.totalPrice = food.quantity * result.market_data.current_price
  }
}
// run function
fetchPrices()

// this console.log show me initial values, without totalPrice key
console.log('foods : ', foods);

I assume that console.log() runs before the async/await function, but I don't know how to add this value in my initial array (or another).

What would be the way to add the key/value totalPrice to each elements of the "foods" array?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda