// 宏任务微任务
// async + await 执行机制
async function async1(){
console.log('async1 start') // 2
// await 后面的函数一定是需要先执行的, 在同步任务中
// 请求得到的结果是异步返回的
// await 前面的返回值和下面的所有代码, 等同于是 promise 的 .then 回调函数第一个参数及内部代码
// .then 回调函数中的代码都是异步的微任务
// async2().then(() => {
// console.log('async1 end')
// })
await async2()
console.log('async1 end') // 6 微任务1
}
async function async2(){
console.log('async2') // 3
}
console.log('script start') // 1
setTimeout(function(){
console.log('setTimeout') // 8 宏任务
},0)
async1();
// new Promise 时传入一个回调函数, 这个回调函数是同步执行的, 创建 promise 对象后立即执行
new Promise(function(resolve){
console.log('promise1') // 4
// resolve 表示这个 promise 成功了, 将来会执行 .then 里的第一个回调函数
resolve();
}).then(function(){
console.log('promise2') // 7 微任务2
})
console.log('script end') // 5 主线程执行完毕
// 正确答案:
// script start
// async1 start
// async2
// promise1
// script end
// async1 end
// promise2
// setTimeout
03-20
264

12-17
1931

05-18
1249
