es6·await/async案例笔记

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>await/async案例笔记</title>
</head>
<body>
	
<script type="text/javascript">
function delayGetTime() {
      
      
    return new Promise((resolve, reject) => {
      
      
        setTimeout(() => {
      
      
            resolve(Date.now())
        }, 2000)
    })
}

async function syncTest() {
      
      
	let result = [];
    result.push(await delayGetTime())
    result.push(await delayGetTime())
    return result
}

(async () => {
      
      
	console.log(1)
	console.log(await syncTest());
	console.log(2)
	// 没有await堵塞又按主线程控制先同步后异步,先宏任务后微任务
	delayGetTime().then(t => console.log(t));
	console.log(3)
})();
</script>
</body>
</html>

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_35606400/article/details/132548586