<!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>