Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

306
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda