weixin_33694172 2018-12-27 09:49 采纳率: 0%
浏览 32

为什么捕获块会运行? [重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/24662289/when-is-thensuccess-fail-considered-an-antipattern-for-promises" dir="ltr">When is .then(success, fail) considered an antipattern for promises?</a>
                            <span class="question-originals-answer-count">
                                (7 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2018-12-27 11:28:52Z" class="relativetime">2 years ago</span>.</div>
        </div>
    </aside>

I have the following function which makes an ajax request o fetch data from an API.

function getSegements(url) {
    return new Promise((resolve, reject) => {
        request = new XMLHttpRequest();
        request.open('GET', url);
        request.setRequestHeader('Content-Type', 'application/json');

        // request.onload = () => resolve(request.response);
        // request.onerror = () => reject(request.status);

        request.onreadystatechange = function() {

            if (request.readyState === 4)
            { 
                if (request.status === 200)
                {
                    data = JSON.parse(request.response);
                    console.log(data.segements);
                    resolve(data); 
                }
                else
                {
                    reject({status: request.status});
                }
            }
        };
        request.send();
    });
}

calling the function:

getSegements(url).then((data) => {
    //console.log(data);
    //data = JSON.parse(data);
    theWheel = new Winwheel({
        'outerRadius'     : 212,
        'textFontSize'    : 16,
        'textOrientation' : 'horizontal',
        'textAlignment'   : 'outer',
        'numSegments'     : data.no,
        'segments'        : data.segements,
        'animation' :           // Specify the animation to use.
        {
            'type'     : 'spinToStop',
            'duration' : 5,     // Duration in seconds.
            'spins'    : 3,     // Default number of complete spins.
            'callbackFinished' : alertPrize
        }
    });
    theWheel.animation.spins = 9;
    wheelSpinning = false;
})
.catch((err)=>{
    console.log(err);
    alert('Request failed.  Returned status of ' + err.status);
});

When there is a fault in WinWheel's parameter it runs the catch block. Why is running like that? Doesn't it depend on the function (in this case getSegements) if then() is going to run or catch()?

</div>
  • 写回答

2条回答 默认 最新

  • weixin_33713350 2018-12-27 09:55
    关注

    then() returns a Promise as well, and uncaught exceptions are propagated through the call chain until catch() is found, and therefore catch() runs for any exception caught in a call chain

    new Promise((res, rej) => {
      res()
    }).then(() => {
      throw "in then"
    }).catch(e => console.log(e))

    </div>
    
    评论

报告相同问题?