In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about which of the nodejs's setTimeout (fn,0) or setImmediate will be implemented first. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
Let's first take a look at the following code
SetTimeout (() = > {console.log ('setTimeout');}, 0)
SetImmediate (() = > {console.log ('setImmedate');})
When we execute the above code, we will find that the output is uncertain. Let's take a look at why.
Nodejs's event cycle is divided into several phases (phase). SetTimeout belongs to the timer phase and setImmediate belongs to the check phase. Sequentially, the timer phase is executed earlier than check. The corresponding implementation principles of these two functions have been introduced in the analysis of nodejs's setImmediate and setTimeout. I won't go into details here. There is a very important detail in the implementation code of setTimeout.
After * = 1; / / coalesce to number or NaN
If (! (after > = 1 & & after TIMEOUT_MAX) {
Process.emitWarning (`${after} does not fit into` +
'a 32-bit signed integer.' +
'\ nTimeout duration was set to 1.'
'TimeoutOverflowWarning')
}
After = 1; / / schedule on next tick, follows browser behavior
}
We found that although the timeout we passed is 0, 0 is not a legal value, and nodejs will change the timeout to 1. This is the reason for the uncertainty of the above code output. Let's analyze the execution of this code. When nodejs starts, it compiles and executes the above code, starts a timer, and mounts a setImmediate node in the queue. Then enter the libuv event loop, and then execute the timer phase. Libuv determines whether 1 millisecond has elapsed since the timer was started. If so, execute the timer callback, otherwise the next node will be executed. After other stages, the check phase will be executed. At this point, the callback of setImmediate will be executed. So, the output of the first piece of code depends on whether or not one millisecond has elapsed between the start of the timer and the timer execution phase of the libuv.
After reading the above, do you have any further understanding of which of nodejs's setTimeout (fn,0) or setImmediate executes first? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.