色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

ES6中Promise、async和await面試題整理

瀏覽:107日期:2022-06-01 16:36:52
目錄
  • 出題目的:
  • 知識點:
  • 代碼:
  • 附:promise與async await結合使用
  • 總結

學習過程中遇到的一些基礎的Promise、async、await面試題整理。

出題目的:

  • 考察 Promise、async、await 的基礎
  • 考察隊Event Loop、宏任務、微任務的理解

知識點:

  • JS 執(zhí)行順序:單線程,自上而下、先同步后異步、先微任務后宏任務
  • new promise() -> Promise.resolve(),觸發(fā)then
  • new promise((reject)=>{reject()}) -> promise.reject(),觸發(fā)catch
  • then 和 catch 內部沒有 throw new Error 相當于 resolve
  • async function 相當于返回 Promise.resolve()
  • await 后面的代碼都是異步的,微任務;setTimeout是宏任務
  • 初始化Promise時,函數(shù)內部代碼會被立即執(zhí)行

代碼:

考點1:Promise.resolve、Promise.reject執(zhí)行順序

Promise.resolve().then(() => {  // 優(yōu)先尋找then
		console.log(1);
	}).catch(() => {
		console.log(2);
	})
	// 1
Promise.reject().then(() => {  // 優(yōu)先尋找catch
		console.log(1);
	}).catch(() => {
		console.log(2);
	})
	// 2

考點2:then 和 catch 內部沒有 throw new Error() 相當于 resolve

Promise.resolve().then(() => {
		console.log(1);
	}).catch(() => {
		console.log(2);
	}).then(() => {
		console.log(3);
	})
	// 1 3
Promise.reject().then(() => {
		console.log(1);
	}).catch(() => {
		console.log(2);
	}).then(() => {
		console.log(3);
	})
	// 2 3
Promise.reject().then(() => {
		console.log(1);
	}).catch(() => {
		console.log(2);
		throw new Error();
	}).then(() => {
		console.log(3);
	})
	// 2 報錯
Promise.reject().then(() => {
		console.log(1);
	}).catch(() => {
		console.log(2);
		throw new Error();
	}).then(() => {
		console.log(3);
	}).catch(() => {
		console.log(4);
	})
	// 2 4

考點3:async function -> 相當于返回一個 Promise.resolve

const res = async function fn() {
	return 100;
}
console.log(res());  // 返回一個resolve狀態(tài)的Promise對象 Promise {<fulfilled>: 100}
res().then(()=>{
	console.log(0);
}).catch(()=>{
	console.log(1);
})
// 0

(async function () {
	const a = fn();
	const b = await fn();
	console.log(a);  // Promise {<fulfilled>: 100}
	console.log(b);  // 100
})()

考點4: await 代碼執(zhí)行順序

async function fn1() {
	console.log("fn1 start");
	await fn2();
	console.log("fn1 end");
}
async function fn2() {
	console.log("fn2 start");
}
console.log("start");
fn1();
console.log("end");
/**
 * 打印順序:
 * start
 * fn1 start
 * fn2 start
 * end
 * fn1 end
 */
async function fn1() {
	console.log("fn1 start");
	await fn2();
	console.log("fn1 end");
	await fn3();
	console.log("fn3 end");
}
async function fn2() {
	console.log("fn2");
}
async function fn3() {
	console.log("fn3");
}
console.log("start");
fn1();
console.log("end");
/**
 * 打印順序:
 * start
 * fn1 start
 * fn2
 * end
 * fn1 end
 * fn3
 * fn3 end
 */

考點5:Promise 與 setTimeout 執(zhí)行順序

console.log("start");
setTimeout(()=>{
	console.log("setTimeout")
});
Promise.resolve().then(()=>{
	console.log("Promise")
})
console.log("end")
/**
 * 打印順序:
 * start
 * end
 * Promise
 * setTimeout
 */
async function fn1() {
	console.log("fn1 start");
	await fn2();
	console.log("fn1 end");  // await后面的代碼為"微任務代碼"
}
async function fn2() {
	console.log("fn2");
}
console.log("start");
setTimeout(()=>{
	console.log("setTimeout");  // 宏任務 
});
fn1();
console.log("end");
/**
 * 打印順序:
 * start
 * fn1 start
 * fn2
 * end
 * fn1 end
 * setTimeout
 */

附:promise與async await結合使用

昨天看了一道字節(jié)外包的面試題

?const list = [1, 2, 3];
? ? const square = num => {
? ? ? ? return new Promise((resolve, reject) => {
? ? ? ? ? ? setTimeout(() => {
? ? ? ? ? ? ? ? resolve(num * num);
? ? ? ? ? ? }, 1000);
? ? ? ? });
? ? }
? ? function test() {
? ? ? ? // 修改這里的代碼
? ? ? ? list.forEach(async x => {
? ? ? ? ? ? const res = await square(x);
? ? ? ? ? ? console.log(res);
? ? ? ? });
? ? }
? ? test()

需要修改的是把同步執(zhí)行的數(shù)組替換成換成異步打印。

在測試以后我們可以-驗證,forEach和for循環(huán)不同的是for循環(huán)可以修改數(shù)組的值,且forEach取不到具體某一項的值,這里的異步說的是每執(zhí)行一次數(shù)組循環(huán),就執(zhí)行一步test()方法,

const list = [1, 2, 3];
const square = num => {
?? ?return new Promise((resolve, reject) => {
?? ??? ?setTimeout(() => {
?? ??? ??? ?resolve(num * num);
?? ??? ?}, 1000);
?? ?});
}
?function test() {
? for(let x of list) {
? ? var res = await square(x)
? ? console.log(res)
? }
}
test()

總結

到此這篇關于ES6中Promise、async和await面試題整理的文章就介紹到這了,更多相關ES6 Promise、async、await面試題內容請搜索以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持!

標簽: JavaScript
主站蜘蛛池模板: 国产在线一区二区三区 | 国产精品免费视频能看 | 欧美大片一级特黄 | 草草免费观看视频在线 | 手机国产精品一区二区 | 中文字幕亚洲在线 | 国产三级a三级三级天天 | 亚洲精品国产男人的天堂 | 亚洲一区中文字幕在线 | 国产成人一区二区三区 | 欧美理论片在线观看一区二区 | 91玖玖 | 老师张开腿让我捅 | 国产91香蕉视频 | 日韩freesex呦交 | 久久亚洲综合中文字幕 | 久久精品在线 | 国产精品高清在线观看地址 | 欧美久久久久久久一区二区三区 | 欧美特黄高清免费观看的 | 成人免费视频在线 | 免费人成网站在线播放 | 精品久久久久久乐 | 久青草国产在线 | 日韩成人免费在线视频 | 香港a毛片免费全部播放 | 国产a级高清版毛片 | 香焦视频在线观看黄 | 免费高清在线爱做视频 | 九九热视频精品 | 欧美高清视频在线观看 | 亚洲在线小视频 | ccyycom草草影院成人91 | 久久精品夜色国产 | 国产 日韩 欧美 在线 | 一级特黄特黄的大片免费 | 91精品一区二区三区在线播放 | 亚洲国产精品欧美日韩一区二区 | 欧美成人综合在线 | 性欧美成人依依影院 | 国产在视频线精品视频二代 |