Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

103
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!