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

146
Views
Why setTimeout(code...,1) is executed first than setTimeout(code... ,0)?

I tested this code and the result I expected was getting the Chicken log first, but I was wrong, everytime I run this code I get the Egg log first, does anybody knows whats going on here?

setTimeout(() => {
  console.log("Egg");
}, 1);

setTimeout(() => {
  console.log("Chicken");
}, 0);

Edit: Notice that this behaves differently if those delays are 101ms and 100ms respectively, so the behavior changes even if the difference on the timer is still 1ms

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

0

The time is in milliseconds that the timer should wait before the specified function or code is executed. 1000ms will be 1 sec and since your input to a parameter was 1ms JS just displayed it in the order it was written because 1ms and 0ms were comparable. Moreover, you can use async-await to wait for tasks that would take longer. For instance,

function timeout(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

async function printOnTime() {{
    console.log("Chicken")
    await timeout(1000); 
    console.log("Egg")
}}


printOnTime();

In this example, Chicken will be printed asap however in order to print Egg it will wait for 1 sec (1000ms) and finally print Egg.

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!