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

307
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
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