AnathanPham / blog

随便写写
1 stars 0 forks source link

Promise.then链式调用顺序 #76

Open AnathanPham opened 2 years ago

AnathanPham commented 2 years ago

第一个p(这里取名 p1_a )实例化后p1_a resolve,p1_a.then 注册了一个微任务,返回了一个新p(这里取名 p1_b )。 p1_b为pendding,因为 p1_a.then 的回调需要执行完后才能确定p1_b的状态。 p1_b.then 给p1_b resolve后的任务队列中添加“注册一个微任务”的回调。

const p1 = new Promise(res=>{
    // setTimeout(res,1000)
    res('p1')
}).then((v)=>{
    console.log(1,v)
}).then((v)=>{
    console.log(2,v)
})
const p2 = new Promise(res=>{
    res('p2')
}).then((v)=>{
    console.log(11,v)
}).then((v)=>{
    console.log(22,v)
})
// 1 'p1'
//  11 'p2'
// 2 undefined
// 22 undefined