【问题标题】:Unable to get property 'then' using promises无法使用承诺“然后”获得财产
【发布时间】:2013-07-12 00:36:16
【问题描述】:

我希望在我的应用中包含最清晰的代码。所以我决定将 xhr 调用和解析从 view.js 中分离出来。 为此,我添加了:

在 View.js 中

this._pagePromises.push(myapp.Services.Foo.getFoo()
.then(
    function success(results) {
      var x = results;
    },
    function error() {
      // TODO - handle the error.
    }
));

在 Services.js 中

Foo:
{   
    getFoo: function () {
        WinJS.xhr({ url: "http://sampleurl.com" }).done(
            function completed(request) {
                //parse request
                var obj = myapp.Parser.parse(request);
                return obj;
            },
            function error(request) {
                // handle error conditions.
            }
        );  
    }
}

但我有这个例外:

0x800a138f - JavaScript 运行时错误:无法获取属性 'then' 未定义或空引用

我想要的是: 在 view.js 中启动 promise 做一些事情并在 getFoo() 完成时更新视图。我这样做的方式不正确,但作为 C# 开发人员,我很难理解这种模式。

编辑: 有我更新的代码:

getFoo: function () {
var promise = WinJS.xhr({ url: myapp.WebServices.getfooUrl() });
    promise.done(
        function completed(request) {
            var xmlElements = request.responseXML;
            var parser = new myapp.Parser.foo();
            var items = parser.parse(xmlElements);
            return items;
        },
        function error(request) {
            // handle error conditions.
        }
    );
    return promise;
}

它解决了我关于 'then' 的问题,但是在“return items”之前调用了“return promise”。所以我的“来电者”只得到了承诺,而不是他的结果。

我错过了什么?

编辑 2: 有正确的方法来做到这一点:

Foo:
{
    getFooAsync: function () {
        return WinJS.Promise.wrap(this.getXmlFooAsync().then(
            function completed(request) {

                var xmlElements = request.responseXML;
                var parser = new myapp.Parser.Foo();
                var items = parser.parse(xmlElements);
                return items;
            }
        ));  
    },

    getXmlFooAsync: function () {
      return WinJS.xhr({ url: "http://sampleurl.com" });
    }
}

【问题讨论】:

    标签: javascript xmlhttprequest winjs promise


    【解决方案1】:

    执行此操作的更紧凑的方法是让您的函数从 WinJS.xhr().then() 返回返回值。它的作用是返回一个承诺,该承诺将通过您的内部完成处理程序的返回值来实现:

    Foo:
    {
        getFooAsync: function () {
            return WinJS.xhr({ url: "http://sampleurl.com" }).then(
                function completed(request) {
                    var xmlElements = request.responseXML;
                    var parser = new myapp.Parser.Foo();
                    var items = parser.parse(xmlElements);
                    return items;
                }
            ));  
        },
    }
    

    然后,调用者可以对从 getFooAsync 获得的承诺使用 then/done,完成的处理程序中的结果将是完成处理程序返回的 items。 (你不会在这个函数中使用 .done ,因为你想要返回一个承诺。)

    这是 Promises-A 中 then 的指定行为,以允许链接。有关这方面的更多信息,请参阅我在 Windows 8 开发人员博客上的帖子,http://blogs.msdn.com/b/windowsappdev/archive/2013/06/11/all-about-promises-for-windows-store-apps-written-in-javascript.aspx

    【讨论】:

    • 没错,这样更好。谢谢。
    猜你喜欢
    • 2012-12-14
    • 2019-04-19
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 2015-04-15
    • 2018-04-11
    相关资源
    最近更新 更多