Quantcast
Channel: jQuery Library for SharePoint Web Services
Viewing all articles
Browse latest Browse all 6517

New Post: Promise chaining

$
0
0

Ricardo,
Your loosing me on what you actually want to do - and truthfull, I think SPService already does it. Your last comment - "realize that nested ajax calls in the traditional completefunc would also get left off the deferred object" - tells me you are doing something more complex than the simple example you listed above.

Re: Use of completefunc and Promise callbacks

Although you should not, you can actually use both a completefunc callback and define a promise callback - but you should realize how those are executed and obviously how to use them effectively. jQuery .ajax() method will do the following when it is done, and in this order:
  1. Resolve the ajax Deferred - which turn executes any of the promise callbacks
  2. If a 'complete' callback is defined - execute it
In my opinion, there is really one true use case where I may want to use both a completefunc callback and promise callbacks - to have the ability to change the output of .ajax() and thus control what the callbacks that are queued up see...

So here is an example that will show you this - both the order in with the callbacks are executed and how you could control the XML Document response for down-stream callbacks:
$().SPServices({
    operation: "GetList",
    listName: "Tasks",
    completefunc: function (xData, Status) {  
        console.log("completefunc()");
        console.log(xData.responseXML.paul); // notice I'm printing a property called "paul" of the XML Document returned by .ajajx()/SPServices
    }
})
.done(function(){
    console.log("done()");
})
.then(function (xml, Status) { 
    console.log("then()", xml, Status);
    xml.paul = "tavares"; // pass data to others in the chain via the .ajax() input params objects
})
.then(function(){
    console.log("then(2)");
})
.done(function(){
    console.log("done(1)");
})
.then(function(){
    console.log("then(3)");
});
The output of this call in the console would be:
done()
then() Document success
then(2)
done(1)
then(3)
completefunc()
tavares

Use of multiple SPServices calls with jQuery .when()

The promise returned by SPServices (which is the one returned by .ajax) can in fact be chained and queued up with $.when(). Here is an example:
$.when(
    // Call 1
    $().SPServices({
        operation: "GetList",
        listName: "Tasks",
        completefunc: function (xData, Status) {  
            console.log("completefunc()");
        }
    })
    .done(function(){
        console.log("done()");
    }),

    // Call 2
    $().SPServices({
        operation: "GetList",
        listName: "Tasks",
        completefunc: function (xData, Status) {  
            console.log("completefunc(2)");
        }
    })
    .done(function(){
        console.log("done(2)");
    })
)
.then(function(){

    console.log("ALL PROMISES DONE!!!!!");

});
Then output of this would be:
done()
completefunc()
done(2)
ALL PROMISES DONE!!!!!
completefunc(2)
This again shows that the deferred callbacks that are queued up will executed serially and in the order that they are added to the queue... In looking at the output you may question why 'complete(2)' executed after 'ALL PROMISES DONE!!! and not after "done(2)". That again is because once the second "done(2)" is executed, the .when() Deferred is resolved, and thus its callbacks are executed prior to givein control back to the last .ajax()...


So... :-)
With all that said and tested - can you post a code sample of something that is not working for you?
I'm really having a hard time understanding what is not working.

/Paul

Viewing all articles
Browse latest Browse all 6517

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>