current position:Home>Analysis of event loop mechanism in JavaScript
Analysis of event loop mechanism in JavaScript
2022-04-29 05:20:22【InfoQ】
Preface
Concept
- Synchronization tasks are similar to what we often call immediate execution functions , You don't need to wait, you can do it directly , You can directly enter the main thread to execute , Similar to normal function calls, etc .
- Asynchronous queues are asynchronous execution functions , similar ajax request , In the process of launching , Will enter an asynchronous queue , When loading into a task , Need to wait , Only then can the return value be processed .
Take a chestnut
console.log('1');
setTimeout(function() {
console.log('4');
}, 0);
Promise.resolve().then(function() {
console.log('2');
}).then(function() {
console.log('3');
});
console.log('5');
TIP
Another chestnut
console.log(1);
setTimeout(function() {
console.log(5);
}, 10);
new Promise(resolve => {
console.log(2);
resolve();
setTimeout(() => console.log(3), 10);
}).then(function() {
console.log(4);
})
console.log(6);
- Different from the example 1 In the middle of promise, Print 2 Is prior to 6 Executive , From this we can know that ,new Promise In the process of execution , In the absence of execution resolve perhaps rejected front , The code executed is the code in the synchronization queue .
- Look again 4,5,3 Execution order of , Performing micro tasks promise Execution callback resolve after , Corresponding then Execute now
- In the printout , Timer 5 Take precedence over ----> It's a micro task promise Macro task timer in 3, Timer 5 This macro task is in promise The micro task queue is then added , stay promise Execution completed then After callback ,promise The macro task in is added to the queue , So in the timer 5 After performing
summary
copyright notice
author[InfoQ],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/116/202204261120132793.html
The sidebar is recommended
- Rich text editor: ckeditor (using ckeditor4 Vue)
- The get request of nginx agent only returns part of the data. The problem is solved
- JavaScript traverses the irregularly nested multi-layer objects, returns the map with "index", and realizes the rapid positioning of sub attributes
- HTTP keep alive details
- [technical update] http / 3 quic Foundation
- Vue to react ----- can the constructor be omitted when using ES6?
- Use of nested HTML
- Vue to react to realize slot function
- When to use react PureComponent
- React 16.6 memo()
guess what you like
Deep understanding of children https://segmentfault.com/a/1190000011527160
This paper solves the cross domain problem Vue + springboot caused by the separation of front and back ends
The difference between shallow copy and deep copy is to use native JavaScript to realize deep copy
Definition of Vue slot
Sorting algorithm in JavaScript
JavaScript implements search algorithm, sequential search and binary search
leetcode20. Valid parentheses, implemented using JavaScript
'interface' declarations can only be used in typescript files in Vue 3.0 + ts resolvent
496 next element larger JavaScript implementation leetcode
leetcode 682. Implementation of JavaScript in baseball game
Random recommended
- Source code analysis, Vue What happens when using (), take initializing vuex as an example
- JavaScript optional chain
- Source code analysis, Vue global API set, del, nexttick, use, mixin, etc
- Brief introduction of Vue source code directory design
- Docxtemplator + Vue + docxtemplator image module free realize the front-end Word Download Text and free image download
- vue 2. What happens when x source code analyzes new vue()
- Vue bidirectional binding principle
- Use and principle of Vue keepalive
- Properties and usage of vuex
- Communication mode between Vue components
- Front end interview Foundation
- Usage scenario and principle of Vue nexttick
- Configuration and use of rich text editor ckeditor4 in Vue
- How does Java randomly get elements from a list
- Study summary of pytext: a search path from NLP research to production
- html+css+JavaScript
- Tunnel http://bt3xna.natappfree.cc Invalid cannot connect to 172.0.0.1:8080 The web service powered by natapp. Is not available on this port cn
- Next JS using react quill
- Vue Gaode map can only have one start point and end point. How to realize it
- Divide and conquer strategy -- find the k-th smallest element in the array
- Singleton mode - front end design mode
- QT sets that child elements do not inherit the parent style
- Using react to realize comment function
- Front end Vue template
- Front end vuejs set sail
- Front end Vue data binding and MVVM
- Vue data proxy and hijacking in front end
- Vue style of front end
- Front end Vue event handling
- Principle of Vue monitoring data change in front end
- Vue calculation attribute and monitoring attribute of the front end
- Front end Vue conditional rendering and list rendering
- The front-end Vue collects form data
- Vue built-in instruction of front end
- Unknown HttpClient factory netty at org. openqa. selenium. remote...
- What is the gutter principle of Ant Design Vue a-row
- Element form account password
- CSS to add horizontal lines around the text
- Solution to vscode Vue project always reporting space or semicolon warning
- Calling setcookie in Ajax fails without any error in the IDE