Synchronous task (macro task, micro task), asynchronous task

First look at the results of the following code

console.log(1)
process.nextTick(function(){
    console.log(2)
})
setTimeout(function(){
    console.log(3)
    process.nextTick(function(){
        console.log(4)
    })
})
Promise.resolve().then(function() {
    console.log(5);
}).then(function() {
    console.log(6)
});Copy the code

The priorities of synchronization tasks, micro tasks, and macro tasks are as follows:

Synchronization Task > Micro Task > Macro TaskCopy the code

Macro tasks include

# The browser Node
I/O
setTimeout
setInterval
setImmediate
requestAnimationFrame

Microtasks include

# The browser Node
process.nextTick
MutationObserver
Promise.then catch finally

Back to the problem

1. The first line is the synchronization task, which has the highest priority, so the first output is 1.setTimeout is a macro task, sosetNextTick prints 2, 4, followed by another microtask. Promise prints 5,6, 5, followed by the macro tasksetTimeout, print 3setThere is a microtask in Timeout, and then execute it, printing 4Copy the code

// So it's 1, 2, 5, 6, 3, 4Copy the code