WinJS Promise在base.js中导致JavaScript异常

时间:2014-07-05 03:40:21

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

我正在尝试在我的WinJS Windows 8 App中实现一项服务,该服务需要调用winjs httpClient。我希望我的服务在等待httpClient返回的承诺时返回一个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
    });
})();

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

1 个答案:

答案 0 :(得分:1)

我只是想通了,我错过了新的'就在WinJS.Promise之前。由于WinJS.Promise是一个类,显然需要新建。错误消息只让我感到困惑。