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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:30:36+00:00 2026-05-27T16:30:36+00:00

edit: this question was closed as a duplicate, but the identified duplicate was asked

  • 0

edit: this question was closed as a duplicate, but the identified duplicate was asked two years after this one, and it contains less detail describing the issue at hand

I need to make a series of N ajax requests without locking the browser, and want to use the jquery deferred object to accomplish this.

Here is a simplified example with three requests, but my program may need to queue up over 100 (note that this is not the exact use case, the actual code does need to ensure the success of step (N-1) before executing the next step):

$(document).ready(function(){

    var deferred = $.Deferred();

    var countries = ["US", "CA", "MX"];

    $.each(countries, function(index, country){

        deferred.pipe(getData(country));
        
    });

 });

function getData(country){

    var data = {
        "country": country  
    };


    console.log("Making request for [" + country + "]");

    return $.ajax({
        type: "POST",
        url: "ajax.jsp",
        data: data,
        dataType: "JSON",
        success: function(){
            console.log("Successful request for [" + country + "]");
        }
    });

}

Here is what gets written into the console (all requests are made in parallel and the response time is directly proportional to the size of the data for each country as expected:

Making request for [US]
Making request for [CA]
Making request for [MX]
Successful request for [MX]
Successful request for [CA]
Successful request for [US]

How can I get the deferred object to queue these up for me? I’ve tried changing done to pipe but get the same result.

Here is the desired result:

Making request for [US]
Successful request for [US]
Making request for [CA]
Successful request for [CA]
Making request for [MX]
Successful request for [MX]

Edit:

I appreciate the suggestion to use an array to store request parameters, but the jquery deferred object has the ability to queue requests and I really want to learn how to use this feature to its full potential.

This is effectively what I’m trying to do:

when(request[0]).pipe(request[1]).pipe(request[2])... pipe(request[N]);

However, I want to assign the requests into the pipe one step at a time in order to effectively use the each traversal:

deferred.pipe(request[0]);
deferred.pipe(request[1]);
deferred.pipe(request[2]);
  • 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-27T16:30:37+00:00Added an answer on May 27, 2026 at 4:30 pm

    With a custom object

    function DeferredAjax(opts) {
        this.options=opts;
        this.deferred=$.Deferred();
        this.country=opts.country;
    }
    DeferredAjax.prototype.invoke=function() {
        var self=this, data={country:self.country};
        console.log("Making request for [" + self.country + "]");
    
        return $.ajax({
            type: "GET",
            url: "wait.php",
            data: data,
            dataType: "JSON",
            success: function(){
                console.log("Successful request for [" + self.country + "]");
                self.deferred.resolve();
            }
        });
    };
    DeferredAjax.prototype.promise=function() {
        return this.deferred.promise();
    };
    
    
    var countries = ["US", "CA", "MX"], startingpoint = $.Deferred();
    startingpoint.resolve();
    
    $.each(countries, function(ix, country) {
        var da = new DeferredAjax({
            country: country
        });
        $.when(startingpoint ).then(function() {
            da.invoke();
        });
        startingpoint= da;
    });
    

    Fiddle http://jsfiddle.net/7kuX9/1/

    To be a bit more clear, the last lines could be written

    c1=new DeferredAjax( {country:"US"} );
    c2=new DeferredAjax( {country:"CA"} );
    c3=new DeferredAjax( {country:"MX"} );
    
    $.when( c1 ).then( function() {c2.invoke();} );
    $.when( c2 ).then( function() {c3.invoke();} );
    

    With pipes

    function fireRequest(country) {
            return $.ajax({
                type: "GET",
                url: "wait.php",
                data: {country:country},
                dataType: "JSON",
                success: function(){
                    console.log("Successful request for [" + country + "]");
                }
            });
    }
    
    var countries=["US","CA","MX"], startingpoint=$.Deferred();
    startingpoint.resolve();
    
    $.each(countries,function(ix,country) {
        startingpoint=startingpoint.pipe( function() {
            console.log("Making request for [" + country + "]");
            return fireRequest(country);
        });
    });
    

    http://jsfiddle.net/k8aUj/1/

    Edit : A fiddle outputting the log in the result window http://jsfiddle.net/k8aUj/3/

    Each pipe call returns a new promise, which is in turn used for the next pipe. Note that I only provided the sccess function, a similar function should be provided for failures.

    In each solution, the Ajax calls are delayed until needed by wrapping them in a function and a new promise is created for each item in the list to build the chain.

    I believe the custom object provides an easier way to manipulate the chain, but the pipes could better suit your tastes.

    Note : as of jQuery 1.8, deferred.pipe() is deprecated, deferred.then replaces it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: This question is a duplicate of What is the difference between managed and
Edit : This question has already been asked and answered, and I apparently am
EDIT: this question is mostly closed and the only problems i have with this
EDIT This question has been closed on SO and reposted on ServerFault https://serverfault.com/questions/333168/how-can-i-make-my-ssis-process-consume-more-resources-and-run-faster I
This question seems to have been asked before, but I feel like my situation
Edit: This question was written in 2008, which was like 3 internet ages ago.
EDIT: This question is more about language engineering than C++ itself. I used C++
EDIT : This question duplicates How to access the current Subversion build number? (Thanks
(EDIT: This question is now outdated for my particular issue, as Google Code supports
Edit This question has gone through a few iterations by now, so feel free

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.