有关Promise.catch方法的理解

合规开
• 阅读 2221

之前写了一篇有关Promisethen方法的文章,现在在讲解一下有关Promise原型上catch的方法


catch方法是为了处理拒绝的情况或者抛出异常的情况

语法

p.catch(onRejected);

p.catch(function(reason){
    // 拒绝
});
参数
  • onRejected:当Promise 被拒绝时,被调用的一个Function,该函数拥有一个参数
  • reson: 拒绝的原因
返回值

一个新的Promise

示例代码

返回的Promise行为状态
var p1 = new Promise(function(resolve, reject) {
  resolve('Success');
});
p1.then(function(value) {
  console.log(value); // "Success!"
  throw 'oh, no!';
}).catch(function(e) {
  console.log(`e`,e); // "e oh, no!"
  //或者
  //Promise.resolve();
}).then(function(value){
    console.log('after a catch the chain is restored',value);
}, function (err) {
  console.log('Not fired due to the catch',err);
});
上面的打印结果为:
Success
e oh, no!
after a catch the chain is restored undefined
var p1 = new Promise(function(resolve, reject) {
  resolve('Success');
});
p1.then(function(value) {
  console.log(value); // "Success!"
  throw 'oh, no!';
}).catch(function(e) {
  console.log(`e`,e); // "e oh, no!"
  //或者
  return Promise.resolve();
}).then(function(value){
    console.log('after a catch the chain is restored',value);
}, function (err) {
  console.log('Not fired due to the catch',err);
});
上面的打印结果为:
Success
e oh, no!
after a catch the chain is restored undefined

若把上面的代码改为以下这样:

var p1 = new Promise(function(resolve, reject) {
  resolve('Success');
});
p1.then(function(value) {
  console.log(value); // "Success!"
  throw 'oh, no!';
}).catch(function(e) {
  console.log(`e`,e); // "e oh, no!"
  //或者
  return Promise.reject("ssssss");
}).then(function(value){
    console.log('after a catch the chain is restored',value);
}, function (err) {
  console.log('Not fired due to the catch',err);
});
上面的打印结果为:
Success
e oh, no!
Not fired due to the catch ssssss
var p1 = new Promise(function(resolve, reject) {
  resolve('Success');
});
p1.then(function(value) {
  console.log(value); // "Success!"
  throw 'oh, no!';
}).catch(function(e) {
  console.log(`e`,e); // "e oh, no!"
  throw "ssssss"
}).then(function(value){
    console.log('after a catch the chain is restored',value);
}, function (err) {
  console.log('Not fired due to the catch',err);
});

上面的打印结果为:
Success
e oh, no!
Not fired due to the catch ssssss

在上面的代码中可以看出catch返回的Promise状态和then方法中的行为是一致的,具体请参考then

抛出一个错误或返回一个失败的 Promise,Promise 通过 catch() 返回失败onRejected状态的结果;否则,它将返回成功的onFulfilld状态的数据

异常捕获的行为
// 抛出一个错误,大多数时候将调用catch方法
var p1 = new Promise(function(resolve, reject) {
  throw 'Uh-oh!';
});

p1.catch(function(e) {
  console.log(e); // "Uh-oh!"
});

// 在异步函数中抛出的错误不会被catch捕获到
var p2 = new Promise(function(resolve, reject) {
  setTimeout(function() {
    throw 'Uncaught Exception!';
  }, 1000);
});

p2.catch(function(e) {
  console.log(e); // 不会执行
});

// 在resolve()后面抛出的错误会被忽略
var p3 = new Promise(function(resolve, reject) {
  resolve();
  throw 'Silenced Exception!';
});

p3.catch(function(e) {
   console.log(e); // 不会执行
});

有关Promise.catch方法的理解

扫描关注,查看更多文章,提高编程能力

点赞
收藏
评论区
推荐文章
blmius blmius
4年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Easter79 Easter79
4年前
swap空间的增减方法
(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap
双十一预售活动分析
2022年双十一促销活动已经开始,大家应该都提前开始关注今年双十一活动的时间表了吧?2022年10月24日晚8:00天猫双11预售时间,第一波销售时间10月31日晚8:0,第二波销售时间11月10日晚8:00;天猫双11的优惠力度是跨店每满30050
Aidan075 Aidan075
4年前
44万条数据揭秘:如何成为网易云音乐评论区的网红段子手?
昨天在凹凸数读写了一篇新文章,有关网易云音乐热评的分析,链接如下:《网易云音乐热评的规律,44万条数据告诉你》(http://mp.wei
AWS国庆双重礼,仅限7天
自2021年10月1日00:00起至2021年10月7日24:00,新注册并激活(需全部完成账号注册的五个步骤,否则账号状态并未激活)AWS海外区域账户,填写页面下方表单,即可申领价值$200美元的AWS海外区域账户服务抵扣券直充到您的账户,用以抵扣服务消费,助您轻松体验多个云迁移应用场景。同时,您还可获赠AWS精美祥云纪念T恤一件。,仅限7天$20
Wesley13 Wesley13
4年前
VSCode配置FiraCode和更纱黑体字体
!(https://oscimg.oschina.net/oscnet/c7bb62d935ceb01d3b7fe176322e84ae00d.png)Fira Code下载到FiraCode字体的GitHub(https://www.oschina.net/action/GoToLink?urlhttps%
Stella981 Stella981
4年前
Python code question 10
Question10Level2Question:Writeaprogramthatacceptsasequenceofwhitespaceseparatedwordsasinputandprintsthewordsafterremovingallduplicatewordsandsortingth
Wesley13 Wesley13
4年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Stella981 Stella981
4年前
Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法
Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法参考文章:(1)Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fwww.codeprj.com%2Fblo
五、飞鹅官网API接口文档
接口列表1.获取网站信息请求方法:GET请求URL:/api/site/getSiteInfo请求参数无返回结果json"code":1,"data":"id":1,//id"title":"SampleSiteName",//网站名称"intro":"T