JavaScript Interview Question #35: What is faster, zero timeout, or immediate resolve?
Coderslang: Become a Software Engineer
Posted on April 22, 2021
What's the order of the output?
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Both setTimeout
and Promise.resolve
are asynchronous actions, which means that the inner console.log
statements will be evaluated after some delay.
The difference is that Promise.resolve
schedules the microtask, and setTimeout
schedules the macrotask. Micro tasks have higher priority than macrotasks, thus Promise.resolve
will be evaluated faster and the first output will be 2
.
ANSWER: 2
will be printed on the first line, followed by 1
.
💖 💪 🙅 🚩
Coderslang: Become a Software Engineer
Posted on April 22, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
beginners JavaScript Interview Question #35: What is faster, zero timeout, or immediate resolve?
April 22, 2021