javascript - Unable to get property 'then' using promises -


i want have clear code app. decided separate xhr call , parsing view.js. added :

in view.js

this._pagepromises.push(myapp.services.foo.getfoo() .then(     function success(results) {       var x = results;     },     function error() {       // todo - handle error.     } )); 

and in 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.             }         );       } } 

but have exception :

0x800a138f - javascript runtime error: unable property 'then' of undefined or null reference

what want there : start promise in view.js stuff , update view when getfoo() completed. i'm not doing right way c# developper have difficulties understand pattern.

edit : there updated code:

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

it solved issue 'then' "return promise" called before "return items". "caller" promise , not result.

what did miss ?

edit 2 : there correct way :

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

a more compact way of doing have function return return value winjs.xhr().then(). return promise fulfilled return value of inner completed handler:

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

the caller can use then/done on promise gets getfooasync, , result in completed handler items returned completed handler. (you not use .done inside function because you're wanting return promise.)

this specified behavior of then in promises-a, allow chaining. more on this, see post on windows 8 developer blog, http://blogs.msdn.com/b/windowsappdev/archive/2013/06/11/all-about-promises-for-windows-store-apps-written-in-javascript.aspx.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -