<script>
console.log(11111) //1
async function async1() {
await async2()
console.log("async1")//7
}
async function async2() {
await async3()
console.log("async2")//5
}
async function async3() {
console.log("async3")//2
}
async1()
setTimeout(() => {
console.log("定时器1")//9
})
new Promise((resolve) => {
console.log("promise")//3
resolve(999);
}).then(() => {
console.log("promise1")//6
}).then((data) => {
console.log("promise2")//8
setTimeout(() => {
console.log(data)//10
})
})
console.log(2222)//4
</script>
//代码的执行顺序
先同步后异步执行
宏微执行.html:13 11111
宏微执行.html:24 async3
宏微执行.html:34 promise
宏微执行.html:45 2222
宏微执行.html:21 async2
宏微执行.html:37 promise1
宏微执行.html:17 async1
宏微执行.html:39 promise2
宏微执行.html:30 定时器1
宏微执行.html:41 undefined
在这里代码是先上后下的顺序执行的,先同步后异步的执行方法。
如果有还不懂的同学建议,多动手检测一下。里面的逻辑有点绕。