总结
-
promise resolve
后,会变为resloved
状态,此时会调用then
方法,调用then
会默认返回一个resolved
状态的promise对象
,会继续调用 then
方法。但是如果 then
里抛出 error
的话,那么会返回一个rejected
状态的promise对象
,会调用catch
方法
-
promise reject
后,会变为rejected
状态,会调用catch
方法,如果catch
里正常结束
的话,会返回一个resolved
状态的promise对象
,会继续调用`then方法
// 同步代码先执行,then里面的是异步函数,后执行
const p1 = Promise.resolve()
console.log('p1',p1)
const p1Then = p1.then(()=>{
console.log('p1 then')
})
console.log('p1Then:',p1Then)
const p2 = Promise.reject('error')
console.log('p2',p2)
const p2Then = p2.then(()=>{
console.log('p2 then')
}).catch(err=>{
console.log(err)
console.log('p2 catch')
})
console.log('p2Then:',p2Then)
p2Then.then(()=>{
console.log('p2Then then')
}).catch(()=>{
console.log('p2Then catch')
})

image.png

image.png
网友评论