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

126
Views
Why does setTimeout of 1 is executing before setTimeout of 0

setTimeout(() => console.log('1'), 1);

setTimeout(() => console.log('4'), 0);

setTimeout(() => console.log('3'), 2);

setTimeout(() => console.log('2'), 0);

I expected the output to be 4,2,1,3 but the output is 1,4,2,3. How the first setTimeout with delay 1 is getting executed even before 0?

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

0

Because the time you used is very less once the code is executed it shows as per the flow.

setTimeout(() => console.log('1'), 2000);

setTimeout(() => console.log('4'), 0);

setTimeout(() => console.log('3'), 3000);

setTimeout(() => console.log('2'), 0);

about 4 years ago · Juan Pablo Isaza Report

0

It is because till, the line number 2 setTimeout(() => console.log('4'), 0); is loading/rendering and converting into binary, before rendering only, the 1 ms is completed, resulting in 1 ms setTimeout() working before 2 ms setTimeout()

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!