Demo of the execution order of macro tasks (setTimeout) and micro tasks (async await) in Day 257/300 JS

(1) Topic

async function async1() {
    
    
  console.log('async1 start')
  await async2()
  console.log('async1 end')
  setTimeout(() => {
    
     console.log('time1') }, 0)
}
async function async2() {
    
    
  console.log('async2 start')
  setTimeout(() => {
    
     console.log('time2') }, 0)
}
async1()
console.log('start')
setTimeout(() => {
    
     console.log('time3') }, 0)

(2) Printing order

async1 start
async2 start
start
async1 end
time2
time3
time1

(3) Analysis

1. Encounter the content of the current line after await (regardless of the statement or function method);
2. Encounter the content of the non-current line after await, which is executed after the function is executed;
3. The macro task queue will print out in sequence according to the order in which the push enters;

Guess you like

Origin blog.csdn.net/xinghuowuzhao/article/details/126654782