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

123
Visualizações
Wait for a function to finish, before the next one executes

I have a react app. In it when a user summits the form, I want to open a modal, and store the users response in state. Based on this reponse I want to preform some business logic, then post the form.

This is my onsubmit method

      async function onSubmit(submission: IPayload) {
        await handleModal();
        await submit(submission);
      }
    
      async function submit(submission: ICreateAdPayload) {
          await api.create({ this is where all the props go})
      }

     // Heres I set the state of the modal to open, which then chanes the open prop on another component for the modal to open.

      async function handleModal(submission: any) {
        setShowModal(true);
    
        // handle logic

        setShowModal(false);
  }

However my submit function is being carried out as soon as the modal opens. Any idea?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

To be able to await handleModal(...) then that method must return a Promise which you resolve when it's work has finished - assuming it itself does not need to await anything, it does NOT need to be marked async

function handleModal(submission: any) {
    setShowModal(true);

    return new Promise(resolve => {
        // handle logic 

        setShowModal(false);

        resolve();
    });
}
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