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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:27:35+00:00 2026-06-01T07:27:35+00:00

I have code that looks something like this in javascript: forloop { //async call,

  • 0

I have code that looks something like this in javascript:

forloop {
    //async call, returns an array to its callback
}

After ALL of those async calls are done, I want to calculate the min over all of the arrays.

How can I wait for all of them?

My only idea right now is to have an array of booleans called done, and set done[i] to true in the ith callback function, then say while(not all are done) {}

edit: I suppose one possible, but ugly solution, would be to edit the done array in each callback, then call a method if all other done are set from each callback, thus the last callback to complete will call the continuing method.

  • 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-01T07:27:36+00:00Added an answer on June 1, 2026 at 7:27 am

    You haven’t been very specific with your code, so I’ll make up a scenario. Let’s say you have 10 ajax calls and you want to accumulate the results from those 10 ajax calls and then when they have all completed you want to do something. You can do it like this by accumulating the data in an array and keeping track of when the last one has finished:

    Manual Counter

    var ajaxCallsRemaining = 10;
    var returnedData = [];
    
    for (var i = 0; i < 10; i++) {
        doAjax(whatever, function(response) {
            // success handler from the ajax call
    
            // save response
            returnedData.push(response);
    
            // see if we're done with the last ajax call
            --ajaxCallsRemaining;
            if (ajaxCallsRemaining <= 0) {
                // all data is here now
                // look through the returnedData and do whatever processing 
                // you want on it right here
            }
        });
    }
    

    Note: error handling is important here (not shown because it’s specific to how you’re making your ajax calls). You will want to think about how you’re going to handle the case when one ajax call never completes, either with an error or gets stuck for a long time or times out after a long time.


    jQuery Promises

    Adding to my answer in 2014. These days, promises are often used to solve this type of problem since jQuery’s $.ajax() already returns a promise and $.when() will let you know when a group of promises are all resolved and will collect the return results for you:

    var promises = [];
    for (var i = 0; i < 10; i++) {
        promises.push($.ajax(...));
    }
    $.when.apply($, promises).then(function() {
        // returned data is in arguments[0][0], arguments[1][0], ... arguments[9][0]
        // you can process it here
    }, function() {
        // error occurred
    });
    

    ES6 Standard Promises

    As specified in kba’s answer: if you have an environment with native promises built-in (modern browser or node.js or using babeljs transpile or using a promise polyfill), then you can use ES6-specified promises. See this table for browser support. Promises are supported in pretty much all current browsers, except IE.

    If doAjax() returns a promise, then you can do this:

    var promises = [];
    for (var i = 0; i < 10; i++) {
        promises.push(doAjax(...));
    }
    Promise.all(promises).then(function() {
        // returned data is in arguments[0], arguments[1], ... arguments[n]
        // you can process it here
    }, function(err) {
        // error occurred
    });
    

    If you need to make a non-promise async operation into one that returns a promise, you can “promisify” it like this:

    function doAjax(...) {
        return new Promise(function(resolve, reject) {
            someAsyncOperation(..., function(err, result) {
                if (err) return reject(err);
                resolve(result);
            });
        });
    }
    

    And, then use the pattern above:

    var promises = [];
    for (var i = 0; i < 10; i++) {
        promises.push(doAjax(...));
    }
    Promise.all(promises).then(function() {
        // returned data is in arguments[0], arguments[1], ... arguments[n]
        // you can process it here
    }, function(err) {
        // error occurred
    });
    

    Bluebird Promises

    If you use a more feature rich library such as the Bluebird promise library, then it has some additional functions built in to make this easier:

     var doAjax = Promise.promisify(someAsync);
     var someData = [...]
     Promise.map(someData, doAjax).then(function(results) {
         // all ajax results here
     }, function(err) {
         // some error here
     });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code that looks something like this: <html> <head> <script type=text/javascript src=http://code.jquery.com/jquery-latest.min.js></script> <script
In my code, I have something that looks like this: @implementation MyClass - (id)
I have code that I'd like to run that looks something like this pseudocode:
I have an tag on my page that looks something like this: <a href=javascript:void();
I have some code that looks something like this: <head runat="server"> <script type="text/javascript"> var
I have some very simple javascript code that looks like this: var newWindow =
I have code that looks something like, #include<stdlib.h> #include<string.h> char** someArray = NULL; size_t
I have a code that looks something like: struct Data { int value; };
I have some code in a couple of different functions that looks something like
I have code that looks like this: template<class T> class list { public: class

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.