async function async1(){
console.log("async1 start")
await async2()
console.log("async1 end")
}
async function async2(){
console.log("async2")
}
setTimeout(function(){
console.log("setTimeout")
},0)
async1();
new Promise(function(resolve){
console.log("promise1")
resolve();
}).then(function(){
console.log("promise2")
})
console.log("script end")
the execution order of the above code is
async1 start -> async2 -> promise1-> script end -> promise2 -> async1 end -> setTimeout
what"s more confusing here is the order of promise2 and async1 end.
see a material saying that when async2 is executed, a promise object will be returned and placed in the task queue, and then when the synchronous task is completed, the promise object just mentioned in the task queue will be executed. then it encounters the resolve function, and then it is put into the task queue and jumps out of the async1 function . Then the console in then is executed, and finally console.log ("async1 end")
is executed. Why do you jump out of the resolve function when you encounter the async1 function in thebold area?