宏任務&微任務

以chrome爲例chrome

setTimeout(function(){    console.log('setTimeout')},0)
new Promise(function(resolve){
    console.log('promise')
    resolve()
}).then(function(){
    console.log('then')
})複製代碼

瀏覽器eventloop機制promise

js執行爲單線程,所以分爲 同步和異步瀏覽器

先執行同步任務,即 setTimeout ,  new Promise,  console.log('promise')bash

在異步時分爲 宏任務 和 微任務,異步

setTimeout,setInterval等爲宏任務,Promise爲微任務。兩個任務被掛載到不一樣的task queue中。oop

瀏覽器先執行微任務的queue,即console.log(‘then’)而後在執行 宏任務 console.log(‘setTimeoout’)ui

相關文章
相關標籤/搜索