javascript|[JavaScript][异步]Promise 构造函数是同步执行还是异步执行,那么 then 方法呢()

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

执行结果是:1243
promise构造函数是同步执行的,then方法本身是同步执行,then方法中的内容加入微任务异步执行
【javascript|[JavaScript][异步]Promise 构造函数是同步执行还是异步执行,那么 then 方法呢()】对上面的例子的扩展:
const promise = new Promise((resolve, reject) => { console.log(1); resolve(5); console.log(2); }).then(val => { console.log(val); }); promise.then(() => { console.log(3); }); console.log(4); setTimeout(function() { console.log(6); });

执行结果: 124536
microtask queue里面先添加的promise.resolve(5).then((val)=>{console.log(val)}),后添加的promise.then(() => {console.log(3); });

    推荐阅读