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

207
Views
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 answers
Answer question

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