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

196
Views
¿Es posible bloquear la ejecución hasta que se completen todos los oyentes de JavaScript anteriores?

Soy bastante nuevo en JavaScript, he buscado mucho en SO pero aún no he encontrado ninguna solución adecuada, que no implique el encadenamiento. Tengo dos oyentes de eventos, registrados en este orden.

El primer oyente ( module_a.js ) cambia un valor de formulario:

 // 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; });

El segundo oyente ( module_b.js ) en realidad envía el formulario:

 // 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 }) });

El segundo oyente debe "esperar" hasta que se complete el primero. O cualquier otro oyente asíncrono completo. No puedo "encadenar" los dos porque están en diferentes módulos.

¿Posible o imposible?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puedes lograr esto usando Promesas.

 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!