Promise 的三种状态,以及then、catch的链式调用

如题所述

第1个回答  2022-07-14

promise的三种状态:pending resolved rejected
1)pending:在过程中,没有结果,不会触发then和catch
2)resolved:已经解决,触发then
3)rejected:已经拒绝,触发catch

resolved状态 reject状态

then和catch状态改变
then正常返回resolved,里面有报错返回rejected
catch正常返回resolved,里面有报错返回rejected

resolve---->then
then 中是一个正常return ,得到resolve ,可以继续执行 .then里面的内容
then 中是一个throw error,得到reject,不能执行后面的 .then
rejected---->catch
catch 中是一个正常return ,得到resolve可以继续执行 .then里面的内容
catch 中是一个error,得到reject ,不能执行后面的 .then,可以执行catch

eg1

结果 1 3
第一个then中成功了,就相当于得到了一个resolve,resolve执行then,不执行catch,2不执行,3执行

eg2

结果 1 2 3
第一个then中有throw err 出错了,相当于reject,reject执行catch,所以catch里面的内容正常执行,catch正常执行了,相当于resolve,resolve会执行then

eg3

结果 1 2
第一个then执行,里面有err ,相当于reject,执行catch,catch成功执行了,相当于resolved,执行then,不执行catch,所以2执行,3不执行

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网