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

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

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