We all know that JS is executed by a single thread, but when it comes to asynchronous operations, it will put the asynchronous operation in the task queue, and wait for the main thread to finish executing, it will execute the asynchronous operation in the task queue. When it comes to task queues, we all know that they are first in, first out. Asynchronous operation also involves macro tasks and micro tasks, I would like to ask you, what are the macro tasks? What are microtasks? Let me show you what the output of this example is

console.log(1);
setTimeout(() =>{
console.log(2)
})
Promise.resolve().then(function() {
console.log(3)
})
Promise.resolve().then(function() {
setTimeout(() =>{
console.log(4)
})
})
setTimeout(() =>{
Promise.resolve().then(function() {
console.log(5)
})
})
new Promise(function(resolve, project) {
console.log(6)
}).then(function() {
console.log(7)
})

Copy the code

The output is:



See the following figure for specific analysis: