[面试题]事件循环经典面试题解析
Python微信訂餐小程序課程視頻
https://edu.csdn.net/course/detail/36074
Python實戰(zhàn)量化交易理財系統(tǒng)
https://edu.csdn.net/course/detail/35475
基礎概念
面試題一
復制代碼- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
javascript`setTimeout(function () {
console.log(“setTimeout1”);
new Promise(function (resolve) {
resolve();
}).then(function () {
new Promise(function (resolve) {
resolve();
}).then(function () {
console.log(“then4”);
});
console.log(“then2”);
});
});
new Promise(function (resolve) {
console.log(“promise1”);
resolve();
}).then(function () {
console.log(“then1”);
});
setTimeout(function () {
console.log(“setTimeout2”);
});
console.log(2);
queueMicrotask(() => {
console.log(“queueMicrotask1”)
});
new Promise(function (resolve) {
resolve();
}).then(function () {
console.log(“then3”);
});`
面試題二
復制代碼- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
javascript`async function async1() {
console.log(‘async1 start’)
// await異步函數的返回結果 resolve的結果會作為整個異步函數的promise的resolve結果->同步代碼
// await后面的執(zhí)行代碼 就會變成.then后面的執(zhí)行函數->微任務
// 也就是說 console.log(‘async1 end’) 這一段是相當于then方法內的 會被加入微任務中
await async2();
console.log(‘async1 end’)
}
async function async2() {
console.log(‘async2’)
}
console.log(‘script start’)
setTimeout(function () {
console.log(‘setTimeout’)
}, 0)
async1();
new Promise(function (resolve) {
console.log(‘promise1’)
resolve();
}).then(function () {
console.log(‘promise2’)
})
console.log(‘script end’)`
面試題三
復制代碼- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
javascript`Promise.resolve().then(() => {
console.log(0);
//1.直接返回4 微任務不會做任何延遲
// return 4
//2.直接返回Promise.resolve(4) 微任務推遲兩次
// return Promise.resolve(4);
//3.返回thenable對象
return {
then: ((resolve, reject) => {
resolve(4);
})
}
}).then((res) => {
console.log(res)
})
Promise.resolve().then(() => {
console.log(1);
}).then(() => {
console.log(2);
}).then(() => {
console.log(3);
}).then(() => {
console.log(5);
}).then(() => {
console.log(6);
})`
這道面試題有些特殊,需要大家記住兩個結論
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-aMWrhIg9-1646846037009)(https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/569c204f981e42c5a98d2014e4594856~tplv-k3u1fbpfcp-zoom-1.image “上述是then中返回的三種情況”)]
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-itzZizEq-1646846037013)(https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6e5976aafab541deace1e7dfdfa55ae6~tplv-k3u1fbpfcp-zoom-1.image “普通返回,不會推遲”)][外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-YB6D2Rjk-1646846037014)(https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6d76aecd1449480ab33d9ffa352ff3e2~tplv-k3u1fbpfcp-zoom-1.image “返回thenable 推遲一次”)][外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-TxUzboTl-1646846037015)(https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/9446c84c4a4846deb833e0b2597642fd~tplv-k3u1fbpfcp-zoom-1.image “返回Promise.resolve,推遲兩次”)]
面試題四
本道題是基于node的事件循環(huán),和瀏覽器的事件循環(huán)不一樣,需要記住以下幾點
node的事件循環(huán)也分宏任務和微任務
- 宏任務: setTimeout、setInterval、IO事件、setImmediate、close事件
- 微任務: Promise的then回調、process.nextTick、queueMicrotask
node的每次事件循環(huán)都是按照以下順序來執(zhí)行的
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
javascript`async function async1() {
console.log(‘async1 start’)
await async2()
console.log(‘async1 end’)
}
async function async2() {
console.log(‘async2’)
}
console.log(‘script start’)
setTimeout(function () {
console.log(‘setTimeout0’)
}, 0)
setTimeout(function () {
console.log(‘setTimeout2’)
}, 300)
setImmediate(() => console.log(‘setImmediate’));
process.nextTick(() => console.log(‘nextTick1’));
async1();
process.nextTick(() => console.log(‘nextTick2’));
new Promise(function (resolve) {
console.log(‘promise1’)
resolve();
console.log(‘promise2’)
}).then(function () {
console.log(‘promise3’)
})
console.log(‘script end’)`
總結
以上是生活随笔為你收集整理的[面试题]事件循环经典面试题解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python Qt GUI设计简介、环境
- 下一篇: 基于ASA防火墙的SSL ×××配置