Here is a question about macrotasks and microtasks: setTimeout(function(){ console.log('1') }); new Promise(function(resolve){ console.log('2'); resolve(); }).then(function(){ console.log('3') }); console.log('4') What is the execution order of the above code?
I guess the thinking is: Don’t I know that js is executed line by line? I was a little panicked, so I pasted it into the browser to take a look: Unbelievable Confused, I can only study the operating mechanism of JavaScript carefully! 1. About JavaScript 2. JavaScript Event LoopSince js is single-threaded, it is like a cafeteria with only one window. Students need to line up to get their meals one by one. Similarly, js tasks must also be executed one by one in sequence. This mode is simple to execute, but as future demands, transactions, and requests increase, the execution efficiency of this single-threaded mode will inevitably be low. As long as one task takes a long time to execute, the subsequent tasks cannot be executed during this time. It is common that high-definition pictures included in news load very slowly. Should our webpage be stuck until the pictures are fully displayed? To solve this problem, the JavaScript language divides task execution modes into synchronous and asynchronous:
If we express the content of the map in words:
With code expression: let data = []; $.ajax({ url:blog.csdn.net, data:data, success:() => { console.log('Sent successfully!'); } }) console.log('Code execution ended'); The above is a simple ajax request code:
I believe that through the above text and code, you have a preliminary understanding of the execution order of js. However, this is also the reason why some friends answered 2, 4, 1, 3. However, in fact, there are still tricks in the asynchronous queue. In our interview question, 3. Macrotasks and microtasksEveryone understands it differently, because macrotasks and microtasks are not standard, but the order of execution is unified in js. Macro Tasks:
Microtasks:
Macro tasks include : <script> overall code, process.nextTick varies too much, different nodes are not executed uniformly, and there is no standard Tip: Some people like to put the entire <script> code in a macro task, but I personally don’t like it. In my case, it is just the main thread of execution. I personally classify both macro tasks and micro tasks into asynchronous tasks! Let’s look at the interview question again: //Callback is an asynchronous task setTimeout(function(){//macro task console.log('1') }); new Promise(function(resolve){ console.log('2');//Synchronize main thread resolve(); }).then(function(){//microtask console.log('3') }); console.log('4') //Synchronize main thread
Therefore: the results are 2, 4, 3, 1! In addition, let’s expand on this: setTimeout(() => {//Macro task queue 1 console.log('1'); //Macro task team 1 task 1 setTimeout(() => {//Macro task queue 3 (macro task in macro task queue 1) console.log('2') }, 0) new Promise(resolve => { resolve() console.log('3') //Macro task queue 1 task 2 }).then(() => {//Microtask in macrotask queue 1 console.log('4') }) }, 0) setTimeout(() => {//Macro task queue 2 console.log('5') }, 0) console.log('6') //Synchronize the main thread
What will the above code output? 4. Expand macrotasks and microtasksThe above question is complicated. You may want to think about it. How should we execute it one by one in this complicated situation?
After verification, the result is correct! This is the end of this article about macro tasks and micro tasks in js. For more relevant macro tasks and micro tasks in js, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Some suggestions for improving Nginx performance
>>: Some summary of MySQL's fuzzy query like
Table of contents Create a global shared content ...
Table of contents Overview Precautions 1. Usage 2...
Edit /etc/docker/daemon.json and add the followin...
The PHP base image used in this article is: php:7...
Table of contents need: Ideas: lesson: Share the ...
Common properties of tables The basic attributes ...
A simple calculator written in WeChat applet for ...
Table of contents Preface Computed properties Int...
Preface The effect problems used in personal actu...
background Today, I was browsing CodePen and saw ...
Select or create a subscription message template ...
Some time ago, I needed to use pip downloads freq...
Table of contents 1. Comments on MySQL primary ke...
Table of contents Preface - Vue Routing 1. The mo...
1 Background Recently, I have been studying how t...