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

305
Views
i want to Call a function after previous function is complete reactjs

I want to call a function when previous function is completed. I use async and await, but it does not work :

  const getDate = async () => {
await Object.keys(baskets || {}).forEach(vendorCode => {
  recalculateBill({
    calculateOrder: true,
    shop: Tools.pickVendorFiled(baskets[vendorCode]),
    shopex: tab === DELIVERY_TYPES.NORMAL
  })
})

getShopexInfo({ ids: basketIds })
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can use for...in loop instead of Object.keys(baskets || {}).forEach, in which await will work as expected:

async function getDate () {
  for (const vendorCode in baskets ) {
    await recalculateBill({ calculateOrder: true,
                            shop: Tools.pickVendorFiled(baskets[vendorCode]),
                            shopex: tab === DELIVERY_TYPES.NORMAL
                           });
  }
  getShopexInfo({ ids: basketIds })
}
about 4 years ago · Juan Pablo Isaza Report

0

Using async/await is quite tricky in forEach loop, maybe you can try switching to for of loop it works great with async/await and you will achieve what you want to Or you also try doing it this way

 const getDate = async () => {
 Object.keys(baskets || {}).forEach(await(vendorCode) => {
  recalculateBill({
    calculateOrder: true,
    shop: Tools.pickVendorFiled(baskets[vendorCode]),
    shopex: tab === DELIVERY_TYPES.NORMAL
  })
})

getShopexInfo({ ids: basketIds })
}
about 4 years ago · Juan Pablo Isaza Report

0

The problem here is that you have a loop, await a loop with inside some promises it's tricky, you need to use Promise.all in order to wait all of them to be finished before continuing with the next function.

Promise All take an array of promises (I use .map and not .forEach to return an array of promises), and return a promise itself so you can just await it.

const getDate = async () => {
    const basketsArr =  Object.keys(baskets || {});

  await Promise.all(basketsArr.map((vendorCode) => 
    await recalculateBill({
      calculateOrder: true,
      shop: Tools.pickVendorFiled(baskets[vendorCode]),
      shopex: tab === DELIVERY_TYPES.NORMAL
    })
  ))
  
  await getShopexInfo({ ids: basketIds })
}
about 4 years ago · Juan Pablo Isaza Report
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!