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 6639963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:37:03+00:00 2026-05-25T23:37:03+00:00

I have been researching jquery deferred objects for a while now and I’m stumped

  • 0

I have been researching jquery deferred objects for a while now and I’m stumped on one thing.

The “done,” “always,” “then,” etc methods take, as their arguments, functions which should be called when the deferred object is resolved.

However I tried chaining requests by passing a deferred object into the “always” method, and that seems to work too:

// A is an array of data
var req = ajax(url + "?d=" + A[0]).done(processResults).fail(reportFailure);
for (var i = 1 ; i < A.length ; i++) {
  var tmp = ajax(url + "?d=" + A[i]).done(processResults).fail(reportFailure);
  req.always(tmp);
  req = tmp;
}

However, it doesn’t really work. Because when I follow the above code with this:

req.always(foobar);

foobar seems to get called at some random time, not necessarily after the last element in the array is processed.

Would it be better to use when?

// A is an array of data
var req = $.when(ajax(url + "?d=" + A[0]).done(processResults).fail(reportFailure));
for (var i = 1 ; i < A.length ; i++) {
  req = $.when(req, ajax(url + "?d=" + A[i]).done(processResults).fail(reportFailure));
}
req.always(foobar);

Would the above code (using “when”) result in the ajax requests happening one after the other, or would they occur simultaneously?

I looked into chaining with “pipe” by the way, but because of scoping issues I think it would be harder to use “pipe” with a for loop as above.

Also, why does the original code “almost work”? What is happening there? Is it executing the deferred object as if it were a function, and what happens when it does that?

  • 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-25T23:37:04+00:00Added an answer on May 25, 2026 at 11:37 pm

    You should build an array and use javascript apply :

    //create an array of one deferred per element
    var requests =  $.map(A, function(index, a){return= $.ajax(url + "?d=" + a);});
    //merge all deferreds into a single one
    var mergedRequest = $.when.apply(null, requests);
    
    mergedRequest.done(function(res0, res1, ...){
       //your code goes here
       //if you want results as an array just use arguments
       arguments.length == A.length;
    });
    

    EDIT:
    If you want your calls to be made serially do :

    var result = $.ajax(url + "?d=" + A[0]);
    for (var i = 1 ; i < A.length ; i++) {
      result = result.pipe(function() {
        return $.ajax(url + "?d=" + a[i]);
      }
    }
    result.done(processResults).fail(reportFailure);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been researching the JQuery way to add table rows dynamically. One excellent
I have been researching this problem for a while now and I just can't
I have a problem I have been researching for a while on the internet
Im new to jQuery and have been researching and playing with it as much
I have been researching FRP and found a bunch of different implementations. One model
I have been researching this problem and while there's lots of posts on various
I have been researching for a while and have actually created a prototype ASP.NET
I have been researching this one for awhile. I found several resources on the
I have been researching asynchronous messaging, and I like the way it elegantly deals
I have been researching and playing with functional programming lately, solely to broaden my

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.