我爱编程

Promise3

2018-06-21  本文已影响3人  郭的妻
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    // 结论
    // 1 .错误不会被重复铺获;
    // 2 .catch返回一个promise实例,并且 是resolve的状态
    // 3 .抛出错误变为reject状态,所以绕过了两个then直接
    // 跑到最下面catch
    console.log("here we go...");
    new Promise(resolve => {
        setTimeout(()=>{
            resolve();
        },2000)
    })
        .then(()=>{
            console.log("start");
            throw new Error ("test error")
        })
        .catch(err=>{
            console.log("I catch:",err)
            throw new Error("another error")
        })
        .then(()=>{
            console.log("arrive here!");
        })
        .then(()=>{
            console.log("here...")
        })
        .catch(err =>{
            console.log("No ,I Catch:",err)
        })
</script>
</body>
</html>
上一篇下一篇

猜你喜欢

热点阅读