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

192
Views
Why the sync code is running after the macrotask

As my understanding about event loop, event loop will push the callbacks into the callstack, but for example, the following code, the sync code console.log(2) is running after the click event handler, why is that?

console.log(1)

document.body.addEventListener('click', () => {
  console.log(3)
})

document.body.click()

console.log(2)

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

By doing :

console.log(1)

document.body.addEventListener('click', () => {
    console.log(3)
})

document.body.click()

console.log(2)

The result is :

1
3
2

But by doing :

console.log(1)

document.body.addEventListener('click', () => {
    setTimeout( () => {
        console.log(3)
    },200);
})

document.body.click()

console.log(2)

The result is :

1
2
3

So my conclusions are that the click event is well put in the stack, but that it unstacks instantly.

about 4 years ago · Santiago Trujillo 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!