狀態:1、pending進行中的狀態,該狀態進行初始化,在過程中還沒有結果;2、fulfilled成功狀態,resolved狀態會觸發後續的then回呼函數;3、rejected失敗狀態, rejected狀態會觸發後續的catch回呼函數。

本教學操作環境:windows10系統、ECMAScript 6.0版、Dell G3電腦。
三種狀態
1.pending:過程中還沒有結果
2.resolved:成功
#3.rejected:失敗
##狀態變化
1、pending -> resolved2、pending -> rejected狀態的表現
#pending狀態不會觸發then和catchresolved狀態會觸發後續的then回呼函數rejected狀態會觸發後續的catch回呼函數#then和catch改變狀態
then正常情況下會傳回resolved,報錯則回傳rejectedcatch正常情況下會傳回resolved,報錯則傳回rejected測試題
//第一题(结果会打印出来1,3,返回resolved状态)
Promise.resolve().then(()=>{
console.log(1) //1 resolved
}).catch(()=>{
console.log(2)
}).then(()=>{
console.log(3) // 3 resolved
})
//第二题(结果会打印出来1,2,3)
Promise.resolve().then(()=>{
console.log(1) //1
throw new Error("error1") //rejected
}).catch(()=>{
console.log(2) //2 resolved
}).then(()=>{
console.log(3) //3 resolved
})
//第三题(结果会打印出来1,2)
Promise.resolve.then(()=>{
console.log(1) //1
throw new Error("error1") //rejected
}).catch(()=>{
console.log(2) //2 resolved
}).catch(()=>{
console.log(3)})
以上是es6中promise物件的狀態有哪些的詳細內容。更多資訊請關注PHP中文網其他相關文章!