locked
function that returns promise not working as it should

    Question

  • Hi

    I cant get my head around this so was hoping someone can help, I have created a function that returns a promise which uses winJS.xhr to go a url and pull back some information. The function needs to run about 10 times one after the other but only run when the previous one has finished. When I run this it works for the first 2 then the last 8 times these run together before the previous one has finished. Can someone tell me what im doing wrong...

    My function is...

                                    

    this.buildChannelListAsync = function (serviceID) {   

                var promise = WinJS.xhr({ url: anyURL });

                promise = promise.then(
                    function (xhr) {
                        if (xhr.status === 200) {
                            var json = JSON.parse(xhr.responseText);
                            do some stuff...
                        }
                    }, 
                    function (error) {
                        // An error occurred, find more info on the parameter
                        console.log('There was an error  ' + error.statusText);
                    },
                    function (progress) {
                        // get progress information here
                        //console.log('progress... '+ progress.readyState);
                    }
                );

                promise.done(
                    function () {        
    do other stuff...                    
                    },
                    function (error) {
                        // An error occurred, find more info on the parameter
                        console.log('There was an error with the feed ' + error.statusText);
                    }
                );
                return promise;
            };

    I call the function using the following code...

    myObj.buildChannelListAsync(dfsfd).then(function () { myObj.getChannelListAsync(sdfs) }).then(function () { myObj.getChannelListAsync(cvbcb) });

    Friday, June 7, 2013 8:06 AM

Answers

  • Sorry, typo on the link :)

    I assume getChannelListAsync has a similar code to buildChannelListAsync and it returns a promise the correct syntax to chain them would be:

    myObj.buildChannelListAsync(dfsfd)
    .then(function () { return myObj.getChannelListAsync(sdfs); })
    .then(function () { return myObj.getChannelListAsync(cvbcb); })
    .then(function () { return myObj.getChannelListAsync(sdfs); }).then(.....);

    Notice the return added to each of the chained promises. That should do the trick. Also, the last promise should be a "done", not a "then" to provide correct error handling.

    Another option is using Promise.join().done() passing the 10 promises to the join method, but that fires the 10 promises and handles the ending of all.

    • Marked as answer by jsn100 Monday, June 10, 2013 7:52 PM
    Friday, June 7, 2013 3:52 PM

All replies

  • Hi,

    I don't see in your code where are you executing 10 times the function, I see a buildChannelListAsync chained with 2 getChannelListAsync (3 promises total), but I dont see the other 7?

    Maybe this article helps: Chaining promises.

    Friday, June 7, 2013 2:06 PM
  • Go on the assumption that I do 10 then statements I didnt want to list them all out e.g.

    myObj.buildChannelListAsync(dfsfd).then(function () { myObj.getChannelListAsync(sdfs) }).then(function () { myObj.getChannelListAsync(cvbcb) }).then(function () { myObj.getChannelListAsync(sdfs) }).then(function () { myObj.getChannelListAsync(cvbcb) }).then(function () { myObj.getChannelListAsync(sdfs) }).then(function () { myObj.getChannelListAsync(cvbcb) }).then(function () { myObj.getChannelListAsync(sdfs) }).then(function () { myObj.getChannelListAsync(cvbcb) });

    The link you have in your reply does not work can repost with correct link.

    Thanks

    jas

    Friday, June 7, 2013 3:26 PM
  • Sorry, typo on the link :)

    I assume getChannelListAsync has a similar code to buildChannelListAsync and it returns a promise the correct syntax to chain them would be:

    myObj.buildChannelListAsync(dfsfd)
    .then(function () { return myObj.getChannelListAsync(sdfs); })
    .then(function () { return myObj.getChannelListAsync(cvbcb); })
    .then(function () { return myObj.getChannelListAsync(sdfs); }).then(.....);

    Notice the return added to each of the chained promises. That should do the trick. Also, the last promise should be a "done", not a "then" to provide correct error handling.

    Another option is using Promise.join().done() passing the 10 promises to the join method, but that fires the 10 promises and handles the ending of all.

    • Marked as answer by jsn100 Monday, June 10, 2013 7:52 PM
    Friday, June 7, 2013 3:52 PM