setTimeout(()=>{
console.log('set1')
})
new Promise((resolve,reject)=>{
console.log('p1')
resolve();
}).then(()=>{
console.log('then1')
})
console.log('1')
The final execution result sequence is: p1, set1, then1, set1 The
entire execution sequence involves two points:
- js execution order
- Microtasks and macrotasks
For the execution order of js, look for it in the order of the code,
- Sequential execution without asynchronous
- Asynchronous put into asynchronous queue
- The first part of the new Promise is executed in accordance with the
above three points. It can be seen from the above three points: first print p1 and then 1; after that because set1 and then1 are put into the asynchronous queue, then execute these after the sequential execution is completed. However, the tasks are divided into micro-heat, en-en-wu, and macro tasks: - After each supportive completion, first check whether there is still a microtask in the microtask queue that has not yet been completed. If so, execute the microtask first and then execute the next macrotask.