• Promise错误处理比较:使用then的第二个参数还是catch


    catch是一个语法糖而己 还是通过then来处理的

    如果在then的第一个函数里抛出了异常,后面的catch能捕获到,而then的第二个函数捕获不到。
    catch是一个语法糖而己 还是通过then 来处理的:

    Promise.prototype.catch = function(fn){
        return this.then(null,fn);
    }
    
    • 1
    • 2
    • 3

    建议总是使用catch方法

    建议总是使用catch方法,而不使用then方法的第二个参数。

    具体代码比较

    在这里插入图片描述

    DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>promisetitle>
      head>
      <body>
        <script>
          const promise = new Promise((resolve, rejected) => {
            throw new Error("test");
          });
    
          //此时只有then的第二个参数可以捕获到错误信息
          promise
            .then(
              (res) => {
                //
              },
              (err) => {
                console.log("1:既有error,又有catch。只有error会被执行");
                console.log("err: " + err);
              }
            )
            .catch((err1) => {
              console.log("catch: " + err1);
            });
    
          //此时catch方法可以捕获到错误信息
          promise
            .then((res) => {
              //
            })
            .catch((err1) => {
              console.log("2:只有catch");
              console.log("catch: " + err1);
            });
    
          //此时只有then的第二个参数可以捕获到Promise内部抛出的错误信息
          promise
            .then(
              (res) => {
                throw new Error("hello");
              },
              (err) => {
                console.log("3: 既有error,又有catch。then里面又抛出error。只有error会被执行");
                console.log("error: " + err);
              }
            )
            .catch((err1) => {
              console.log("catch: " + err1);
            });
    
          //此时只有then的第二个参数可以捕获到Promise内部抛出的错误信息
          promise.then(
            (res) => {
              throw new Error("hello");
            },
            (err) => {
              console.log("4: 只有error,没有catch,then里面又抛出error。只有error会被执行");
              console.log("error: " + err);
            }
          );
    
          //此时catch可以捕获到Promise内部抛出的错误信息
          promise
            .then((res) => {
              throw new Error("hello");
            })
            .catch((err1) => {
              console.log("5: 只有catch,then里面又抛出error");
              console.log("catch: " + err1);
            });
        script>
      body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
  • 相关阅读:
    【yolo算法道路井盖检测】
    C语言练习百题之#include应用
    Vuex使用方式及异步问题处理
    【教3妹学编程-算法题】区分黑球与白球
    音视频开发成长之路与音视频知识点总结
    【Spring】核心部分之IOC:通过列举代码例子,从底层刨析,深入源码,轻轻松松理解Spring的核心IOC,IOC有这一篇足以
    为什么HashMap头插法会造成死循环?
    第一章 作业【数据库原理】
    奇瑞金融:汽车金融行业架构设计
    冥想第六百零三天
  • 原文地址:https://blog.csdn.net/qq_27575627/article/details/128016215