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

206
Visualizações
Is possible to block the execution until all previous JavaScript listeners completes?

I'm quite new to JavaScript, I've searched a lot in SO but I have not find any suitable solution yet, not involving chaining. I have two event listeners, registered in this order.

First listener (module_a.js) changes a form value:

// First listener: handle submit and change recaptcha value
form.addEventListener('submit', async refreshRecaptcha() {
  const { target: form } = e;
  const recaptchaField = form.querySelector('[name="catpcha"]');

  // Simulate long running task
  await new Promise(resolve => setTimeout(resolve, 5000));

  recaptchaField.value = token;
});

Second listener (module_b.js) actually submit the form:

// Second listener: POST data to server, with the recaptchaField changed
form.addEventListener('submit', async postData(e) {
  e.preventDefault();

  // Post data
  await fetch(url, { method: 'POST', body: formData })
});

Second listener should "wait" until the first completes. Or any other async listeners complete. I cannot "chain" together the two because they are in different modules.

Possible or impossible?

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

0

You can achieve this using Promises.

 const myPromise = new Promise((resolve, reject) => { setTimeout(() => { resolve('foo'); }, 300); }); myPromise .then(handleResolvedA, handleRejectedA) .then(handleResolvedB, handleRejectedB) .then(handleResolvedC, handleRejectedC);
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