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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:01:07+00:00 2026-05-13T17:01:07+00:00

I stumbled on a piece of Ajax code that is not 100% safe since

  • 0

I stumbled on a piece of Ajax code that is not 100% safe since it’s mixing asynchronous/synchronous type of code… so basically in the code below I have a jQuery.each in which it grabs information on the elements and launch an Ajax get request for each:

$(search).each(function() {
 $.ajax({
  url: 'save.x3?id='+$(this).attr("id")+'value='$(this).data("value");
  success: function(o){
   //Update UI
  },
  error: function(o){
   //Update UI
  }
 });
});

//code to do after saving...

So obviously the ‘code to do after saving…’ often gets executed before all the requests are completed. In the ideal world I would like to have the server-side code handle all of them at once and move //code to do after saving in the success callback but assuming this is not possible, I changed the code to something like this to make sure all requests came back before continuing which I’m still not in love with:

var recs = [];
$(search).each(function() {
 recs[recs.length] = 'save.x3?id='+$(this).attr("id")+'value='$(this).data("value");
});

var counter = 0;
function saveRecords(){
 $.ajax({
  url: recs[counter],
  success: function(o){
   //Update progress
   if (counter<recs.length){
    counter++;
    saveRecords();
   }else{
    doneSavingRecords();
   }
  },
  error: function(o){
   //Update progress
   doneSavingRecords(o.status);
  }
 });
}

function doneSavingRecords(text){
 //code to do after saving...
}

if (recs.length>0){
 saveRecords();  //will recursively callback itself until a failed request or until all records were saved
}else{
 doneSavingRecords();
}

So I’m looking for the ‘best’ way to add a bit of synchronous functionality to a series of asynchronous calls ?

Thanks!!

  • 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-13T17:01:08+00:00Added an answer on May 13, 2026 at 5:01 pm

    Better Answer:

    function saveRecords(callback, errorCallback){
      $('<div></div>').ajaxStop(function(){
        $(this).remove(); // Keep future AJAX events from effecting this
        callback();
      }).ajaxError(function(e, xhr, options, err){
        errorCallback(e, xhr, options, err);
      });
    
      $(search).each(function() {
        $.get('save.x3', { id: $(this).attr("id"), value: $(this).data("value") });
      });
    }
    

    Which would be used like this:

    saveRecords(function(){
       // Complete will fire after all requests have completed with a success or error
    }, function(e, xhr, options, err){
       // Error will fire for every error
    });
    

    Original Answer: This is good if they need to be in a certain order or you have other regular AJAX events on the page that would affect the use of ajaxStop, but this will be slower:

    function saveRecords(callback){
      var recs = $(search).map(function(i, obj) {
       return { id: $(obj).attr("id"), value: $(obj).data("value") };
      });
    
      var save = function(){
       if(!recs.length) return callback();
    
       $.ajax({
        url: 'save.x3',
        data: recs.shift(), // shift removes/returns the first item in an array
        success: function(o){
         save();
        },
        error: function(o){
         //Update progress
         callback(o.status);
        }
       });
      }
    
      save();
    }
    

    Then you can call it like this:

    saveRecords(function(error){
       // This function will run on error or after all 
       // commands have run
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've stumbled upon a piece of code that uses a function std::__throw_logic_error to throw
I stumbled over some code that appears to be correct. It is supposed to
While working with annotations I stumbled accross the following piece of code (it's the
Stumbled on some old code that is throwing and empty catching some cast exceptions
Ever stumbled on a tutorial that you feel is of great value but not
I just stumbled over a weird piece of code while refactoring. It looks like
I've been reading some OSS code lately and stumbled upon this peculiar piece: class
I stumbled upon this piece of code which seems totaly broken to me, but
I stumbled upon this piece of code today: CGRect rect = {{0,0},{w,h}}; Here, I
Hi all I stumbled upon this piece of code today and I am confused

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.