Promise 构造函数是同步执行还是异步执行,那么 then 方法呢?

185 阅读1分钟

解析:
promise构造函数是同步执行的,then方法是异步执行的

验证code:

const promise = new Promise((resolve, reject) => {  
    console.log(1)  
    resolve()  
    console.log(2)  
})  
   
promise.then(() => {  
    console.log(3)  
})  
   
console.log(4)

由于Promise 的then方法是一个异步微任务,而 浏览器中的事件循环机制中确定, 同步代码会优先于异步微任务执行