An Interest In:
Web News this Week
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
- April 17, 2024
- April 16, 2024
April 22, 2021 12:25 pm GMT
Original Link: https://dev.to/coderslang/javascript-interview-question-35-what-is-faster-zero-timeout-or-immediate-resolve-3agl
JavaScript Interview Question 35: What is faster, zero timeout, or immediate resolve?
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
.
Original Link: https://dev.to/coderslang/javascript-interview-question-35-what-is-faster-zero-timeout-or-immediate-resolve-3agl
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To