javascript - WinJS Promise 导致 base.js 中的 JavaScript 异常

标签 javascript windows-phone-8 windows-store-apps winjs

我正在尝试在我的 WinJS Windows 8 应用程序中实现一个服务,该服务需要调用 winjs httpClient。我希望我的服务在等待 httpClient 返回的 promise 时返回 promise 。我的服务代码如下

(function () {
    "use strict";
    var _httpClient = new Windows.Web.Http.HttpClient();
    var _infoUri = new Windows.Foundation.Uri("https://news.google.com/");

    var _getLatestInfo = function () {
        return WinJS.Promise(function (completeCallback, errorCallback, progressCallback) {
            // invoke the httpclient here
            _httpClient.getAsync(_infoUri)
            .then(function complete(result) {
                completeCallback(result);
            }, function error(result) {
                errorCallback(result);
            }, function progress(result) {
                progressCallback(result);
            });
        });
     };

    WinJS.Namespace.define("DataService", {
        getLatestInfo: _getLatestInfo
    });
})();

我按如下方式调用我的服务方法

(function () {
    "use strict";

    WinJS.UI.Pages.define("/pages/home/home.html", {
        // This function is called whenever a user navigates to this page. It
        // populates the page elements with the app's data.
        ready: function (element, options) {
            // TODO: Initialize the page here.
            DataService.getLatestInfo().then(function () { }, function () { }, function () { });
        }
    });
})();

这不起作用,我收到这样的错误

Exception was thrown at line 2018, column 13 in ms-appx://microsoft.winjs.2.0/js/base.js
0x800a01b6 - JavaScript runtime error: Object doesn't support property or method '_setState''

我尝试按如下方式简化我的服务,但没有成功。

(function () {
    "use strict";

    var _getLatestInfo = function () {
        return WinJS.Promise(function (a, b, c) { });
    };

    WinJS.Namespace.define("DataService", {
        getLatestInfo: _getLatestInfo
    });
})();

我不知道该错误试图告诉我什么以及如何纠正它。

最佳答案

我刚刚发现,我错过了 WinJS.Promise 之前的"new"。由于 WinJS.Promise 是一个类,因此显然需要更新。错误消息只会让我更加困惑。

关于javascript - WinJS Promise 导致 base.js 中的 JavaScript 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24582743/

相关文章:

javascript - 如何使用 Rollup 将 js 文件作为字符串导入?

windows-phone-8 - Windows 8 和 Windows Phone 8 无线安装应用程序

c# - 在锁屏下运行 Windows Phone 8.1 RT 应用程序

windows-phone-7 - 按电话号码搜索联系人,使用 3 位前缀过滤

c# - 如何在文本区域的文本末尾显示省略号 (...)?

c# - 在 Windows 运行时从位图创建流

c# - aspx页面VS2012的javascript函数中未命中断点

javascript - 如果绑定(bind)到特定列表项的数组为空,则在 ListView 中显示该特定列表项

javascript - 在 Aurelia 中访问 DOM 元素

c# - Windows Phone 8 检测屏幕解锁