javascript - 失败的 Promise 被 Application.onerror 函数捕获

标签 javascript windows-phone-8.1 promise winjs winjs-promise

有一种奇怪的行为,我不知道它是否应该发生,或者它是 错误。

我正在使用WinJS.UI.Pages.render在特定元素下呈现页面内容。

我面临的问题是,无论我是否处理 render 函数的失败,“错误”都会不断传播,并被onerror“捕获”。功能。

这是我的代码的快速 View :

function goFunction() {
    WinJS.UI.Pages.render('otherpage.html', root).then(null, failedNavigating);
}

function noGoFunction() {
    WinJS.UI.Pages.render('missingpage.html', root).then(null, failedNavigating);
}

function failedNavigating() {
    console.log('failed to navigate');
}

WinJS.Application.onerror = function (args) {
    debugger;
};

otherpage.html 页面存在,因此渲染被执行,一切都很好。但 missingpage.html 不存在。在这种情况下,failedNavigating 函数会执行,到目前为止一切顺利,但随后 WinJS.Application.onerror 处理程序会被“调用”。

这应该是这样的吗?我错过了什么?

这是完整的 project附上复制品,以防有人想看一下。

最佳答案

我终于在你的 GitHub 问题中调试了这个问题。这是因为 WinJS.UI.Pages.render 隐式创建一个 Page,默认情况下它有一个创建错误 promise 的错误处理程序,应用程序将其检测为“未处理的错误 promise ”,因为它没有继续执行任何操作。

您现在可以通过以下方式解决这个问题:

function noGoFunction() {

    WinJS.UI.Pages.define('missingpage.html', {
        error: function (e) {
            console.log("Page failed to load");
        }

    });

    WinJS.UI.Pages.render('missingpage.html', root).then(null, failedNavigating);
}

function failedNavigating() {
    console.log('failed to navigate');
}

关于javascript - 失败的 Promise 被 Application.onerror 函数捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26227251/

相关文章:

c# - Windows Phone 8.1 XAML 广告中介

javascript - 如何捕获自动播放的DOMException?

javascript - 暂停页面执行,直到 jQuery 警报关闭后,然后运行函数

Javascript 单击并拖动以旋转

javascript - 数组中的 $pull 对象,以及另一个数组中的 $pull 引用

windows-phone-8 - Windows(手机)模拟器 8.1/10 在加载操作系统时卡住了

c# - 在 Windows Phone 8.1 中使用文件选择器保存图像

javascript - Apache Cordova、SQLite、从 [表] 中删除,其中 id=?不返回

javascript - Promise.all() 每个 promise 响应

javascript - 重试失败的异步/ promise 功能?