Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested Promises in WinJS

Tags:

winjs

I'm implementing a DataAdapter to bind to a WinJS ListView control. That interface requires a getCount method which returns a Promise. If I return a WinJS.xhr object directly, that works fine. However, I want to massage the response of that before passing it back to the caller.

My problem is that if I return that same WinJS.xhr object, but tack a "then" at the end of it, which takes it's output and massages it, then the caller is not getting the expected output (things blow up in the bowels of the WinJS libraries).

So, I think I don't understand how to return a Promise that's nested in a Promise. Anyone know how to do this?

like image 495
Tom Lianza Avatar asked Jan 30 '26 21:01

Tom Lianza


1 Answers

This is pretty straightforward once you figure it out.

Two things you need to know:

  1. Calling .then() on a promise returns a new promise,
  2. the return value of the function you pass to then becomes the value for that new promise.

So, to do what you want to accomplish, it's be something like this:

return WinJS.xhr({url: whateverYourURlIs })
    .then(function (response) {
        var tweakedResponse = processResponse(response);
        return tweakedResponse;
    });
like image 166
Chris Tavares Avatar answered Feb 03 '26 09:02

Chris Tavares