locked
Promise XHR not work.

    Question

  • function loadlist() {
        new WinJS.Promise(function () {
            WinJS.xhr({ url: "http://abc.com/list/" }).then(function (result) {
                // Promise not work here.
                if (result.status == 200) {
                    var allLink = [];
                    /*
                    Parse result to allLink
                    allLink = [
                        "http://abc.com/list/a.html",
                        "http://abc.com/list/b.html",
                        "http://abc.com/list/c.html"
                    ]
                    */
                    $("#status").html("Total: " + allLink.length);
                    var i = 0, tdName = [], tdAge = [];
                    allLink.map(function (url) {
                        $("#status2").html("Loading: " + i + "/" + allLink.length);
                        WinJS.xhr({ url: url }).then(function (content) {
                            /*
                                Parse content to tdName & tdAge
                                json stringify and save
                            */
                        });
                        i++;
                    });
                } else {
                    console.log('xhr fail');
                }
            }, function (result) {
                console.log(result.statusText);
            }, function (result) {
                console.log(result.readyState);
            });
        });
    }
    Some one help please, xhr in xhr not work with promise.
    Saturday, December 20, 2014 11:21 AM

Answers

  • Hi nhattuanbl,

    I read your code again and found there is a loop and you do the async action in the loop, which will definitely cause some problem, I would suggest you to go through the blog: Async Iteration Pattern (using WinJS promises) and modify your code to see if this works.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by nhattuanbl Tuesday, December 23, 2014 1:28 PM
    Tuesday, December 23, 2014 9:55 AM
    Moderator

All replies

  • Hi nhattuanbl,

    Try to use .done() instead of .then() here.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, December 22, 2014 8:50 AM
    Moderator
  • Replace all .then() to .done() not work.
     It make all another action stop working while this promise runing.
    • Edited by nhattuanbl Monday, December 22, 2014 11:33 AM
    Monday, December 22, 2014 11:31 AM
  • Hi nhattuanbl,

    I read your code again and found there is a loop and you do the async action in the loop, which will definitely cause some problem, I would suggest you to go through the blog: Async Iteration Pattern (using WinJS promises) and modify your code to see if this works.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by nhattuanbl Tuesday, December 23, 2014 1:28 PM
    Tuesday, December 23, 2014 9:55 AM
    Moderator