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

  • SEARCH
  • Home
  • 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 8868205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:12:50+00:00 2026-06-14T17:12:50+00:00

I’m trying to sync up multiple ajax callbacks using jQuery.Deferrd objects. Obviously jQuery.when handles

  • 0

I’m trying to sync up multiple ajax callbacks using jQuery.Deferrd objects. Obviously jQuery.when handles this for you however my code is architected in such a way that the ajax requests aren’t called in the same method. So for example this is the flow:

// A Button is clicked

// Module 1 requests a snippet of html and updates the DOM

// Module 2 requests a different snippet of html and updates the DOM

I need both Modules to update the DOM at the same time meaning I need to ensure the callbacks are run after both requests have returned.

Module 1 and Module 2 need to be able to exist without each other and should have no knowledge of one another so the requests can’t be made together using $.when(doMod1Request(), doMod2Request()).then(function () { … }) and the callbacks should be independent too.

I’ve therefore written a wrapper around ajax which adds the callbacks to a deferred object and in a similar way to $.when resolves the deferred object once the ajax requests have returned the same number of times as the number of callbacks on the deferred object.

My dilemma is however deferred.resolve() can only be called with one set of arguments so each callback get’s the same value.

e.g.

var deferred = new $.Deferred();
deferred.done(function (response) {
    console.log(response); // <div class="html-snippet-1"></div>
});
deferred.done(function (response) {
    console.log(response); // <div class="html-snippet-1"></div>
});
deferred.resolve('<div class="html-snippet-1"></div>');

Whereas I’d want something like this:

var deferred = new $.Deferred();
deferred.done(function (response) {
    console.log(response); // <div class="html-snippet-1"></div>
});
deferred.done(function (response) {
    console.log(response); // <div class="html-snippet-2"></div>
});
deferred.resolve(['<div class="html-snippet-1"></div>', '<div class="html-snippet-2"></div>']);

Is this possible or am I going about this incorrectly?

  • 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-06-14T17:12:51+00:00Added an answer on June 14, 2026 at 5:12 pm

    I’d say this is perfectly valid. Assuming your independent modules, you would do (with two Promises):

    doMod1Request().done(doMod1Update);
    doMod2Request().done(doMod2Update);
    

    Now, if you want to to execute the updates together and only if the two requests both succeeded, just write

    $.when(doMod1Request(), doMod2Request()).done(function(mod1result, mod2result) {
        doMod1Update(mod1result);
        doMod2Update(mod2result);
    });
    

    This only gets ugly if you call your resolve functions with multiple arguments, as jQuery is a bit inconsistent there and does not really distinguish multiple arguments from one array argument.

    To uncouple them with that publish-subscribe pattern you are using, I’d recommend the following:

    function Combination() {
        this.deferreds = [];
        this.success = [];
        this.error = [];
    }
    Combination.prototype.add = function(def, suc, err) {
        this.deffereds.push(def);
        this.success.push(suc);
        this.error.push(err);
    };
    Combination.prototype.start = function() {
        var that = this;
        return $.when.apply($, this.deferreds).always(function() {
             for (var i=0; i<that.deferreds.length; i++)
                 that.deferreds[i].done(that.success[i]).fail(that.error[i]);
             // of course we could also call them directly with the arguments[i]
        });
    };
    
    // Then do
    var comb = new Combination();
    window.notifyModules("something happened", comb); // get deferreds and handlers
    comb.start();
    
    // and in each module
    window.listen("something happended", function(c) {
        c.add(doRequest(), doUpdate, doErrorHandling);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.