then, catch, finally如何影响返回的Promise实例状态

您的关注意义重大

原创@前端司南

虽然Promise是开发过程中使用非常频繁的一个技术点,但是它的一些细节可能很多人都没有去关注过。我们都知道,.then, .catch, .finally都可以链式调用,其本质上是因为返回了一个新的Promise实例,而这些Promise实例现在的状态是什么或者将来会变成什么状态,很多人心里可能都没个底。我自己也意识到了这一点,于是我通过一些代码试验,发现了一些共性。如果您对这块内容还没有把握,不妨看看。

阅读本文前,您应该对Promise有一些基本认识,比如:

  • Promisepending, fulfilled, rejected三种状态,其决议函数resolve()能将Promise实例的状态由pending转为fulfilled,其决议函数reject()能将Promise实例的状态由pending转为rejected

  • Promise实例的状态一旦转变,不可再逆转。

本文会从一些测验代码入手,看看Promise的几个原型方法在处理Promise状态时的一些细节,最后对它们进行总结归纳,加深理解!

先考虑then的行为

==========

then的语法形式如下:

p.then(onFulfilled[, onRejected]);

onFulfilled可以接受一个value参数,作为Promise状态决议为fulfilled的结果,onRejected可以接受一个reason参数,作为Promise状态决议为rejected的原因。

  • 如果onFulfilledonRejected不返回值,那么.then返回的Promise实例的状态会变成fulfilled,但是伴随fulfilledvalue会是undefined

new Promise((resolve, reject) => {

resolve(1)

}).then(value => {

console.log('resolution occurred, and the value is: ', value)

}, reason => {

console.log('rejection occurred, and the reason is: ', reason)

}).then(value => {

console.log('resolution of the returned promise occurred, and the value is: ', value)

}, reason => {

console.log('rejection of the returned promise occurred, and the reason is: ', reason)

})

  • 如果onFulfilledonRejected返回一个值x,那么.then返回的Promise实例的状态会变成fulfilled,并且伴随fulfilledvalue会是x。注意,一个非Promise的普通值在被返回时会被Promise.resolve(x)包装成为一个状态为fulfilledPromise实例。

new Promise((resolve, reject) => {

reject(2)

}).then(value => {

console.log('resolution occurred, and the value is: ', value)

}, reason => {

console.log('rejection occurred, and the reason is: ', reason)

return ‘a new value’

}).then(value => {

console.log('resolution of the returned promise occurred, and the value is: ', value)

}, reason => {

console.log('rejection of the returned promise occurred, and the reason is: ', reason)

})

  • 如果onFulfilledonRejected中抛出一个异常,那么.then返回的Promise实例的状态会变成rejected,并且伴随rejectedreason是刚才抛出的异常的错误对象e

new Promise((resolve, reject) => {

resolve(1)

}).then(value => {

console.log('resolution occurred, and the value is: ', value)

throw new Error(‘some error occurred.’)

}, reason => {

console.log('rejection occurred, and the reason is: ', reason)

}).then(value => {

console.log('resolution of the returned promise occurred, and the value is: ', value)

}, reason => {

console.log('rejection of the returned promise occurred, and the reason is: ', reason)

})

  • 如果onFulfilledonRejected返回一个Promise实例p2,那么不管p2的状态是什么,.then返回的新Promise实例p1的状态会取决于p2。如果p2现在或将来是fulfilled,那么p1的状态也随之变成fulfilled,并且伴随fulfilledvalue也与p2进行resolve(value)决议时传递的value相同;

new Promise((resolve, reject) => {

resolve(1)

}).then(value => {

console.log('resolution occurred, and the value is: ', value)

return Promise.resolve(‘a fulfilled promise’)

}, reason => {

console.log('rejection occurred, and the reason is: ', reason)

}).then(value => {

console.log('resolution of the returned promise occurred, and the value is: ', value)

}, reason => {

console.log('rejection of the returned promise occurred, and the reason is: ', reason)

})

这个逻辑同样适用于rejected的场景。也就是说,如果p2的状态现在或将来是rejected,那么p1的状态也随之变成rejected,而reason也来源于p1进行reject(reason)决议时传递的reason

new Promise((resolve, reject) => {

reject(1)

}).then(value => {

console.log('resolution occurred, and the value is: ', value)

}, reason => {

console.log('rejection occurred, and the reason is: ', reason)

return new Promise((resolve, reject) => {

setTimeout(() => {

reject(‘a promise rejected after 3 seconds.’)

}, 3000)

})

}).then(value => {

console.log('resolution of the returned promise occurred, and the value is: ', value)

}, reason => {

console.log('rejection of the returned promise occurred, and the reason is: ', reason)

})

再考虑catch的行为

===========

catch的语法形式如下:

p.catch(onRejected);

.catch只会处理rejected的情况,并且也会返回一个新的Promise实例。

.catch(onRejected)then(undefined, onRejected)在表现上是一致的。

事实上,catch(onRejected)从内部调用了then(undefined, onRejected)。

  • 如果.catch(onRejected)onRejected回调中返回了一个状态为rejectedPromise实例,那么.catch返回的Promise实例的状态也将变成rejected

new Promise((resolve, reject) => {

reject(1)

}).catch(reason => {

console.log('rejection occurred, and the reason is: ', reason)

return Promise.reject(‘rejected’)

}).then(value => {

console.log('resolution of the returned promise occurred, and the value is: ', value)

}, reason => {

console.log('rejection of the returned promise occurred, and the reason is: ', reason)

})

  • 如果.catch(onRejected)onRejected回调中抛出了异常,那么.catch返回的Promise实例的状态也将变成rejected

new Promise((resolve, reject) => {

reject(1)

}).catch(reason => {

console.log('rejection occurred, and the reason is: ', reason)

throw 2

}).then(value => {

console.log('resolution of the returned promise occurred, and the value is: ', value)

}, reason => {

console.log('rejection of the returned promise occurred, and the reason is: ', reason)

})

  • 其他情况下,.catch返回的Promise实例的状态将是fulfilled

最后看看finally

===========

不管一个Promise的状态是fulfilled还是rejected,传递到finally方法的回调函数onFinally都会被执行。我们可以把一些公共行为放在onFinally执行,比如把loading状态置为false

注意,onFinally不会接受任何参数,因为它从设计上并不关心Promise实例的状态是什么。

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:前端)


核心竞争力,怎么才能提高呢?

成年人想要改变生活,逆转状态?那就开始学习吧~

万事开头难,但是程序员这一条路坚持几年后发展空间还是非常大的,一切重在坚持。

为了帮助大家更好更高效的准备面试,特别整理了《前端工程师面试手册》电子稿文件。

前端面试题汇总

JavaScript

性能

linux

前端资料汇总

完整版PDF资料免费分享,只需你点赞支持,动动手指点击此处就可免费领取了

前端工程师岗位缺口一直很大,符合岗位要求的人越来越少,所以学习前端的小伙伴要注意了,一定要把技能学到扎实,做有含金量的项目,这样在找工作的时候无论遇到什么情况,问题都不会大。

log.csdnimg.cn/img_convert/d7f6750332c78eb27cc606540cdce3b4.png)

linux

前端资料汇总

完整版PDF资料免费分享,只需你点赞支持,动动手指点击此处就可免费领取了

前端工程师岗位缺口一直很大,符合岗位要求的人越来越少,所以学习前端的小伙伴要注意了,一定要把技能学到扎实,做有含金量的项目,这样在找工作的时候无论遇到什么情况,问题都不会大。

  • 24
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值