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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办