Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7607545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:41:26+00:00 2026-05-31T00:41:26+00:00

I have been reading about jQuery’s deferreds, but I can’t quite grasp how to

  • 0

I have been reading about jQuery’s deferreds, but I can’t quite grasp how to use them.

I have the following deeply nested code

Repository.Projects.GetStages(function (data) {
    var stagesXml = $.parseXML(data.d);

    Repository.Projects.GetBenefits(function (data) {
        var benefitsXml = $.parseXML(data.d);

        Repository.Projects.GetPriorities(function (data) {
            var prioritiesXml = $.parseXML(data.d);

            Repository.Projects.GetDifficulties(function (data) {
                var difficultiesXml = $.parseXML(data.d);

                Repository.Projects.GetFactors(function (data) {
                    var factorsXml = $.parseXML(data.d);

                    Repository.Projects.GetRatings(function (data) {
                        var ratingsXml = $.parseXML(data.d);

                        Repository.Projects.GetProjectRatings(selectedPersonIdEncrypted, passDate, function (data) {
                            var dataDoc = UTL.Utility.prototype.setDomDocument(data.d);
                            var xsltDoc = UTL.Utility.prototype.setXslt("Xslt/UserRating/ProjectRatings.xslt");
                            var html = UTL.Utility.prototype.transform(dataDoc, xsltDoc, [
                                ['stages', stagesXml],
                                ['benefits', benefitsXml],
                                ['priorities', prioritiesXml],
                                ['difficulties', difficultiesXml],
                                ['factors', factorsXml],
                                ['ratings', ratingsXml]
                            ]);

                            $('#Project', $content).html(html);
                        });
                    });
                });
            });
        });
    });
});

Each Repository.Projects.* method contains an asynchronous call to get data. The function passed in is the callback, which is passed the resulting data on success. The Repository handles the errors with a generic function, so I don’t need to pass in an error function. I need to ensure each of these has been called, and the data returned, prior to the xslt transformation.

The methods in Repository.Projects.* all look like this

GetStages: function (successCallback) {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "DataRepository.asmx/GetStages",
        cache: false,
        data: JSON.stringify({}),
        dataType: "json",
        success: successCallback,
        error: Repository.FailureCallback
    });
}

It seems like I should be able to rewrite this, I just can’t see how.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:41:28+00:00Added an answer on May 31, 2026 at 12:41 am

    You want all of your functions under Repository.Projects.* to return the $.ajax call (which itself returns a deferred instance).

    GetStages: function() {
      return $.ajax(...);
    }
    

    Then you can use this code:

    $.when(
      Repository.Projects.GetStages()
      ,Repository.Projects.GetBenefits()
      ,Repository.Projects.GetPriorities()
      ,Repository.Projects.GetDifficulties()
      ,Repository.Projects.GetFactors()
      ,Repository.Projects.GetRatings()
    )
      .then(done, fail)
    ;
    
    // success function
    function done(stageResponse, benefitResponse, ...) {
      /*
      each param is the success callback from jquery.ajax.success
      arguments are [ data, textStatus, jqXHR ] 
      */
      var 
        stageXML = $.parseXML(stageResponse[0].d)
        ,benefitXML = $.parseXML(benefitResponse[0].d)
        ...
      ;
    
      Repository.Projects.GetProjectRatings(...)
    }
    
    // error function
    function fail() { }
    

    EDIT:

    You could make this cleaner by wrapping the Repository.Projects.* with their own deferred objects.

    function extractResult($ajax) {
      return $.Deferred(function(dfd) {
        $ajax
          .done(function(response) {
            dfd.resolve(response.d);
          })
          .fail(function(jqXhr) {
            // pass stuff to the failed function
            dfd.reject(...);
          })
        ;
      }).promise();
    }
    

    Keep in mind the cleaner implementation won’t work with the code above. The success function will only receive the contents that it needs to pass to $.parseXML. So you could change the done function variables to $.parseXML(stageResponse).

    If the only thing Repository.Projects.Get* functions do is make $.ajax requests, I’d get rid of them entirely.

    Sexy way:

    $.when.apply($
      ,$.map([
        // these could be directly replaced with calls to $.ajax(...)
        Repository.Projects.GetStages()
        ,Repository.Projects.GetBenefits() 
        ,Repository.Projects.GetPriorities()
        ,Repository.Projects.GetDifficulties()
        ,Repository.Projects.GetFactors()
        ,Repository.Projects.GetRatings()
      ], exctractResult)
    )
       .done(function(stageResponse, benefitResponse, ...) {
         var 
           stageXML = $.parseXML(stageResponse)
           ,benefitXML = $.parseXML(benefitResponse)
           ...
         ;
    
         ...
       })
     ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use jQuery. I have been reading a lot about selector performance and optimizing
I have been reading a lot about JQuery's deferred object. And I can see
I have been reading here and there about multiple JQuery selectors but I couldn't
I have been reading about app engine but I still dont know what I
I have been reading about UnitOfWork pattern for last two days but didn't understand
I have been reading about this but I have to do some thing. I
I have been reading about streams in Java the past days. After reading quite
I have been reading about Reliability Features in .NET and have written the following
I have been reading about the thread-pool pattern and I can't seem to find
i have been reading about proximity security devices through bluetooth, but i am wondering

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.