locked
Chaining WinJS calls RRS feed

  • Question

  • Good Afternoon,

    I have a situation where I have several web service calls for my Windows 8 App. The second call depends on the results of first call, the third call depends on the results of the second call... and so on.

    If I call WinJS.Xhr they all return a different times. I could nest them, but everything I have read about promises says that's a bad idea. I just can't get anything to work. Can someone put a quick skeleton code sample together for me?

    Thank you!

    Friday, July 12, 2013 9:37 PM

Answers

  • Hi,

    Please refer to Chaining promises for a detailed documentation.

    Basically, what you need to do is:

    WinJS.xhr({/* your current options for the first call */}).then(function(result){
    //handle result
    return WinJS.xhr({/* options for the second call */});
    }).then(function(secondResult){
    //handle second result
    return WinJS.xhr({/* options for the third call */});
    }).then(function(thirdResult){
    //handle third result
    return WinJS.xhr({/* options for the fourth call */});
    }).done(function(fourthResult){
    //handle fourth call
    });

    That's an example of chaining 4 xhr's that depend on each other, always remember to end the chain with a "done". If you just want to fire N xhr's on parallel and do something when all of them finish (in any order), you can take a look at WinJS.Promise.join.

    • Marked as answer by RSwit1985 Monday, July 15, 2013 7:27 PM
    Friday, July 12, 2013 10:03 PM