es6中promise对象的状态有哪些

发布时间:2022-05-15 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了es6中promise对象的状态有哪些脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

状态:1、pending进行中的状态,该状态进行初始化,在过程中还没有结果;2、fulfilled成功状态,resolved状态会触发后续的then回调函数;3、rejected失败状态,rejected状态会触发后续的catch回调函数。

本教程操作环境:windows10系统、ECMAScript 6.0版、Dell G3电脑。

es6中promise对象的状态有哪些

三种状态

  • 1.pending:在过程中还没有结果

  • 2.resolved:成功

  • 3.rejected:失败

状态变化

1、pending -> resolved

2、pending -> rejected

状态的表现

pending状态不会触发then和catch

resolved状态会触发后续的then回调函数

rejected状态会触发后续的catch回调函数

then和catch改变状态

then正常情况下会返回resolved,报错则返回rejected

catch正常情况下会返回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)})

21.png

【相关推荐:javascript视频教程、web前端】

以上就是es6中promise对象的状态有哪些的详细内容,更多请关注脚本宝典其它相关文章!

脚本宝典总结

以上是脚本宝典为你收集整理的es6中promise对象的状态有哪些全部内容,希望文章能够帮你解决es6中promise对象的状态有哪些所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: