[面试题]事件循环经典面试题解析
Python微信訂餐小程序課程視頻
https://edu.csdn.net/course/detail/36074
Python實(shí)戰(zhàn)量化交易理財(cái)系統(tǒng)
https://edu.csdn.net/course/detail/35475
基礎(chǔ)概念
面試題一
復(fù)制代碼- 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”);
});`
面試題二
復(fù)制代碼- 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(‘a(chǎn)sync1 start’)
// await異步函數(shù)的返回結(jié)果 resolve的結(jié)果會(huì)作為整個(gè)異步函數(shù)的promise的resolve結(jié)果->同步代碼
// await后面的執(zhí)行代碼 就會(huì)變成.then后面的執(zhí)行函數(shù)->微任務(wù)
// 也就是說(shuō) console.log(‘a(chǎn)sync1 end’) 這一段是相當(dāng)于then方法內(nèi)的 會(huì)被加入微任務(wù)中
await async2();
console.log(‘a(chǎn)sync1 end’)
}
async function async2() {
console.log(‘a(chǎn)sync2’)
}
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’)`
面試題三
復(fù)制代碼- 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 微任務(wù)不會(huì)做任何延遲
// return 4
//2.直接返回Promise.resolve(4) 微任務(wù)推遲兩次
// return Promise.resolve(4);
//3.返回thenable對(duì)象
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);
})`
這道面試題有些特殊,需要大家記住兩個(gè)結(jié)論
[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來(lái)直接上傳(img-aMWrhIg9-1646846037009)(https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/569c204f981e42c5a98d2014e4594856~tplv-k3u1fbpfcp-zoom-1.image “上述是then中返回的三種情況”)]
[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來(lái)直接上傳(img-itzZizEq-1646846037013)(https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6e5976aafab541deace1e7dfdfa55ae6~tplv-k3u1fbpfcp-zoom-1.image “普通返回,不會(huì)推遲”)][外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來(lái)直接上傳(img-YB6D2Rjk-1646846037014)(https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6d76aecd1449480ab33d9ffa352ff3e2~tplv-k3u1fbpfcp-zoom-1.image “返回thenable 推遲一次”)][外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來(lái)直接上傳(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)不一樣,需要記住以下幾點(diǎn)
node的事件循環(huán)也分宏任務(wù)和微任務(wù)
- 宏任務(wù): setTimeout、setInterval、IO事件、setImmediate、close事件
- 微任務(wù): Promise的then回調(diào)、process.nextTick、queueMicrotask
node的每次事件循環(huán)都是按照以下順序來(lái)執(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(‘a(chǎn)sync1 start’)
await async2()
console.log(‘a(chǎn)sync1 end’)
}
async function async2() {
console.log(‘a(chǎn)sync2’)
}
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’)`
總結(jié)
以上是生活随笔為你收集整理的[面试题]事件循环经典面试题解析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Python Qt GUI设计简介、环境
- 下一篇: 基于ASA防火墙的SSL ×××配置