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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:08:20+00:00 2026-06-02T22:08:20+00:00

I have this code that will load css/js/jst (javascript templates) using $.ajax and I’d

  • 0

I have this code that will load css/js/jst (javascript templates) using $.ajax and I’d like to make it run a request after request, running only one at a time, and doing it asynchronously.

var getStuff = function(resources, progressCallback, errorCallback, successCallback, doneCallback){

  var deferreds = [];
  var deferreds_progressCallback = [];
  var len = resources.length;

  for(var idx = 0; idx < len; idx++){

    var resource = resources[idx];

    var dfd = jQuery.Deferred(function(self){
      self.notify(resource);
      (function(resource){
        jQuery.ajax({
          cache: false,
          url: resource,
          dataType: 'text',
          error: function(xhr, status, error){
            self.reject(resource);
          },
          success: function(data, status, xhr){
            if(typeof(data) != 'string'){
              console.log(typeof(data));
            }
            if(resource.match(/.css$/)){
              $('head').append("<style type='text/css'>" + data + "</style>");
            }else if(resource.match(/.jst$/)){
              var templates_div = jQuery("<div style='display:none;'></div>").appendTo('body');
              templates_div.append(data);
            }else if(resource.match(/.js$/)){
              jQuery.globalEval(data);
            }
            self.resolve(resource);
          }
        });
      })(resource);
    });

    deferreds.push(dfd);
    deferreds_progressCallback.push(resource);

  }

  deferreds.reverse();
  deferreds_progressCallback.reverse();

  (function iterateWhen(){
    if(len--){
      jQuery.when(deferreds[len])
      .then(function(resource){
        successCallback(resource);
          iterateWhen();
      })
      .fail(function(resource){
        errorCallback(resource);
      })
      .progress(function(resource){
        progressCallback(resource);
      });
    }else{
      doneCallback && doneCallback();
    }
  }());

}

Right now that code will run all the request at startup. It will wait for the deferred.resolve() to be called, but it won’t wait for the iterateWhen() loop to start iterating over the deferreds[].

According to the jQuery documentation $.Deferred() will accept a function as an optional argument and will run that function before returning from the constructor, so that’s the problem, but I’m not aware of a solution that will do what I want.

Regards!

  • 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-02T22:08:22+00:00Added an answer on June 2, 2026 at 10:08 pm

    I finally did it this way:

    function getStuff(resources, progressCallback, errorCallback, successCallback, doneCallback){
    
      function inject(resource){
        var dfd = jQuery.Deferred();
        dfd.notify(resource);
    
        jQuery.ajax({
          cache: false,
          url: resource,
          dataType: 'text',
          error: function(xhr, status, error){
            dfd.reject(resource);
          },
          success: function(data, status, xhr){
            if(resource.match(/.css$/)){
              $('head').append("<style type='text/css'>" + data + "</style>");
            }else if(resource.match(/.jst$/)){
              $('body').append("<div style='display:none;'>" + data + "</div>");
            }else if(resource.match(/.js$/)){
              jQuery.globalEval(data);
            }
            dfd.resolve(resource);
          }
        });
    
        return dfd;
      };
    
      var len = resources.length;
      resources.reverse();
    
      (function iterateWhen(){
        if(len--){
          inject(resources[len])
          .then(function(resource){
            successCallback && successCallback(resource);
            iterateWhen();
          })
          .fail(function(resource){
            errorCallback && errorCallback(resource);
          })
          .progress(function(resource){
            progressCallback && progressCallback(resource);
          });
        }else{
          doneCallback && doneCallback();
        }
      }());
    
    }
    

    So you just need to call it like this:

    getStuff([
    
      /*Load .JS, .JST and .CSS files*/
      /*Only same-domain request!*/
      'foo/bar.js',
      'bar/foo.css'
    
    ], function(resource){ /*Progress callback*/
    
      console.log("Loading " + resource);
    
    }, function(resource){ /*Errors callback*/
    
      console.log("Error while loading " + resource);
    
    }, function(resource){ /*OK callback*/
    
      console.log("Loaded " + resource);
    
    }, function(){
    
      console.log("Finished loading .js, .jst and .css files!");
    
    });
    

    Regards!

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

Sidebar

Related Questions

i have this code that will check the array contains the specific string and
I have this code that will check for if the class more-results are in
I have the this code that will create excel file and work sheet then
I have this code that I want to make point-free; (\k t -> chr
I have this code that fetches some text from a page using BeautifulSoup soup=
I have a mobile app that I developed using PhoneGap (and html/css/javascript) and there
I have this code that works in a unit test but doesn't work when
I have this code that changes the opacity of the div on hover. $(#navigationcontainer).fadeTo(slow,0.6);
I have this code that has one button that let's me choose an entry
I have this code that saves a pdf file. FileStream fs = new FileStream(SaveLocation,

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.