Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

123
Vistas
Why isnt the event listener going off more than once?
const target = document.getElementById("button");

function when( event) {
  return new Promise((resolve) => {
    target.addEventListener(event, resolve);
  });
}

when( "click").then(() => {
  alert("button clicked");
});

I dont understand why this event listener function wouldnt go off when you click more than once. Despite being in a promise, the event listener is "registered" to the button. So I feel like it should always go off when clicked.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The function when returns a promise. This promise will resolve(and cause the alert) when event listener calls. Because a promise can only resolve once, the alert only triggers at the first time. After that, you can see console.log('event listener calls') when you click button but the alert would not trigger again.

const target = document.getElementById("button");

function when( event) {
  return new Promise((resolve) => {
    target.addEventListener(event, function() {
      console.log('event listener calls');
      resolve();
    });
  });
}

when( "click").then(() => {
  alert("button clicked");
});
<button id="button">Click Me!</button>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda