promise异常传递,中断promise链

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>promise异常传递,中断promise链</title>
</head>
<body>
    <script>
     

            new Promise((resolve,rejected) =>{
                rejected(1)

            }).then(
                value =>{
                    console.log('onResolved1()'+value);
                    return 2
                },
                // reason => {throw reson} //省略了
            ).then(
                value =>{
                console.log('onResolved2()'+value);
               return 3

            },
             // reason => {throw reson} //省略了
            ).then(
                value =>{
                console.log('onResolved3()'+value);

            },
            reason =>{
                return Promise.reject(reason)
            }
            ).catch(reason =>{
                console.log(reason);

                // return new Error('111')
                // return Promise.reject(reason)
                // throw reason

                return new Promise(() =>{}) //返回一个pending状态的promise 中断promise链(后面的.then()不会执行)

               
            }).then(
              value =>{
                console.log('onResolved4()'+value);
              },
              reason =>{
                console.log('onRejected4()'+reason);

              }

            )

        

    </script>
    
</body>
</html>

  

posted @ 2021-02-18 18:37  前端那点事  阅读(14)  评论(0编辑  收藏  举报