Quantcast
Viewing all articles
Browse latest Browse all 6517

New Post: Promise chaining

In order to provide consistent chaining from the returned promise of the core $.SPServices function, I had to implement the always() method instead of the $.ajax complete() method.

I realize that I could omit the complete function from the original $.SPServices call and use the .always() chain in my own code, but since $.SPServices is returning a promise I thought it might be a good idea to be consistent in the callback chain.

The problem was the complete callback in the ajax call does not get pushed into the promise que, so it wouldn't be included in any .then() chaining.

Simple example:
$().SPServices({
                        operation: "GetList",
                        listName: "some list",
                        completefunc: function (xData, Status) {  
                               console.log("I run when I want"); }
                    })
                    .then(function (xml, Status) { 
                           console.log(" I obey promise"); });
Here is the patched code.
            // Finally, make the Ajax call
            promisesCache[msg] = $.ajax({
                // The relative URL for the AJAX call
                url: ajaxURL,
                // By default, the AJAX calls are asynchronous.  You can specify false to require a synchronous call.
                async: opt.async,
                // Before sending the msg, need to send the request header
                beforeSend: function (xhr) {
                    // If we need to pass the SOAPAction, do so
                    if(WSops[opt.operation][1]) {
                        xhr.setRequestHeader("SOAPAction", SOAPAction);
                    }
                },
                // Always a POST
                type: "POST",
                // Here is the SOAP request we've built above
                data: msg,
                // We're getting XML; tell jQuery so that it doesn't need to do a best guess
                dataType: "xml",
                // and this is its content type
                contentType: "text/xml;charset='utf-8'",
                /*
                complete: function(xData, Status) {
                    // When the call is complete, call the completefunc if there is one
                    if($.isFunction(opt.completefunc)) {
                        opt.completefunc(xData, Status);

                    }
                }
                */
            }).always(function (data, Status, xData) {
                // When the call is complete, call the completefunc if there is one
                if ($.isFunction(opt.completefunc)) {
                    opt.completefunc(xData, Status);
                }
            });

Viewing all articles
Browse latest Browse all 6517

Trending Articles