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

115
Views
Javascript Event Loop - Why Javascript Event Handler Work Differently

please take a look at blow code

const btn = document.createElement('button');
btn.innerText = 'button'
document.body.appendChild(btn)

btn.addEventListener('click', () => {
  setTimeout(() => {
   console.log('step1');
  },0)

  Promise.resolve().then(() => {
   console.log('step2')
  })
})
btn.addEventListener('click', () => {
  setTimeout(() => {
  console.log('step3');
 },0)

  Promise.resolve().then(() => {
   console.log('step4')
 })
})

when I click Button element, i can see what i expected as blow

"step2" "step1" "step4" "step3"

but console result is different as blow

"step2" "step4" "step1" "step3"

I don't understand why this occur. give me any answer please

about 4 years ago · Juan Pablo Isaza
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!