3-8 Promise 构造函数是同步还是异步执行,then呢?


Promise 构造函数是同步的,而then 方法是异步的

如何证明?


const p = new Promise((res, rej) => {
    console.log('11111') // 1

    res('bbbb')
})

console.log('899090') // 2

p.then(res => console.log('then 执行;'+ res)) // 3

看见上面的顺序就已经很明白了, 由于Promise 的then方法是一个微任务,而 浏览器中的事件循环机制中确定, 同步代码会优先于微任务执行

相关