瀏覽器中的事件循環:javascript
nodejs中的事件循環css
事件循環的本質java
setTimeout(()=>{ console.log('setTimeout') },0) Promise.resolve().then(() =>{ console.log('promise') }) console.log('main') // main promise setTimeout
javascript爲何是單線程的?node
任務隊列面試
宏任務和微任務promise
事件循環:每執行完一輪宏任務和微任務就叫作一環事件;瀏覽器
setTimeout(() => { console.log('setTimeout') setTimeout(() => { console.log('setTimeout2') },0) },0) Promise.resolve().then(() =>{ console.log('Promise') Promise.resolve().then(() =>{ console.log('Promise2') }) }) console.log('main') // main promise promise2 setTimeout
setTimeout(() => { // 兩個setTimeout誰先進的話 誰就先出 console.log('setTimeout') Promise.resolve().then(() =>{ console.log('promise') }) },0) Promise.resolve().then(() =>{ console.log('promise2') setTimeout(() => { console.log('setTimeout2') },0) }) console.log('main') // main promise2 setTimeout promise setTimeout2
new Promise((res, rej) =>{ console.log(1) res() }).then(() =>{ console.log(2) new Promise((res,rej) =>{ console.log(3) res() }).then(() =>{ console.log(4) }).then(() =>{ console.log(5) }).then(() =>{ console.log(6) }) }).then(() =>{ console.log(7) }) .then(() =>{ console.log(8) }) // 1 2 3 4 7 5 8 6