Questions about macroTasks and microTasks task queues for event polling

console.log("1")

setTimeout(()=>{

    console.log("2")
    Promise.resolve().then(()=>{
    
        console.log(11)
        
    })
    
},0)
setTimeout(function (){

    console.log("10")
    Promise.resolve().then(()=>{
    
        console.log(12)
        
    })
    
},0)
Promise.resolve().then(()=>{

    console.log(3)
    setTimeout(function (){
    
        console.log("8")
        
    },0)
    setTimeout(function (){
    
        console.log("9")
        
    },0)
    Promise.resolve().then(()=>{
    
        console.log(4)
        
    })
    console.log(5)
    
}).then(function (){

    console.log(7)
    
})

console.log(6)

the execution result of the above code in the browser is in line with the expected 1, 6, 3, 5, 4, 7, 11, 10, 10, 12, 8, and 9.

but the result of execution in node environment is unstable and does not match the claims of macroTask and microTask. What is the cause?

May.22,2021

I have read an article before. The author has tested it under different browsers and found that the running results of different browsers are different.
the article is as follows:
https://jakearchibald.com/201.

my view is that there is no need to struggle too much. Different browsers and different versions of node may have different priority processing logic for asynchronous events. A large and comprehensive statement does not exist and must be analyzed on a case-by-case basis. When I read it before, I also wondered which statement was right, or which one was the most comprehensive. Later, I found that it was a bit of a pain in the neck, so I suggest you not to worry about it.


take a look at this question https://codeshelper.com/q/10.

Menu