Search code examples
javascriptwindows-phone-8windows-store-appswinjs

WinJS Promise causes JavaScript exception in base.js


I am trying to implement a service inside of my WinJS Windows 8 App, the service needs to call winjs httpClient. I want my service to return a promise while it waits for the promise returned by httpClient . My service code is as follows

(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
    });
})();

And I call my service method as follows

(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 () { });
        }
    });
})();

This does not work and I get an error like this

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''

I tried simplifying my service as follows with no luck.

(function () {
    "use strict";

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

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

I don't know what the error is trying to tell me and how to correct this.


Solution

  • I just figured it out, I was missing the 'new' just before WinJS.Promise. Since WinJS.Promise is a class it apparently need to be newed. The error message only confused me more.