6-Promise异常穿透现象是怎么回事

<!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>Document</title>
  </head>
  <body>
    <script>
      let p = new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve("Ok");
          // reject("Error");
        }, 1000);
      });

      p.then((value) => {
        console.log(111);
      })
        .then((value) => {
          // console.log(222);
          throw "出错了!!";
        })
        .then((value) => {
          console.log(333);
        })
        .catch((reason) => {
          //中间不用写异常的处理结果,只需要在最后面写catch(then也可以)方法来处理错误的结果就行
          console.warn(reason);
        });
    </script>
  </body>
</html>
posted @ 2022-01-02 16:38  问某完红  阅读(37)  评论(0)    收藏  举报