宏任务:
定时器,ajax,读取文件
微任务:
promise.then
(只有 then 是异步的,放在 new Promise()里的代码是同步的)
async 是 new promise 的简写
1 2 3 4 5 6 7 8 9 10 11
|
async function fn() { return 1 }
function fn() { return new Promise((resolve) => { resolve(1) }) }
|
await 关键字
1 2 3 4 5 6 7 8 9 10 11 12
| async function fun1() { let data = await fun2() console.log(data) }
async function fun2() { console.log(200) return 100 }
fun1()
|
综合题:
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
| console.log(1) async function async1(){ await async2() console.log(2) } async function async2(){ console.log(3) } async1() setTimeout(function(){ console.log(4) },0) new Promise(resolve=>{ console.log(5) resolve() }).then(function(){ console.log(6) }).then(function()=>{ console.log(7) }) console.log(8)
|
读者来信
✎ 投递您的来信