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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:34:22+00:00 2026-06-04T15:34:22+00:00

I need to call an async function (with loop) for multiple values and wait

  • 0

I need to call an async function (with loop) for multiple values and wait for those results. Right now I’m using the following code:

(function(){
    var when_done = function(r){ alert("Completed. Sum of lengths is: [" + r + "]"); }; // call when ready

    var datain = ['google','facebook','youtube','twitter']; // the data to be parsed
    var response = {pending:0, fordone:false, data:0}; // control object, "data" holds summed response lengths
        response.cb = function(){ 
            // if there are pending requests, or the loop isn't ready yet do nothing
            if(response.pending||!response.fordone) return; 
            // otherwise alert.
            return when_done.call(null,response.data); 
        }

    for(var i=0; i<datain; i++)(function(i){
        response.pending++; // increment pending requests count
        $.ajax({url:'http://www.'+datain[i]+'.com', complete:function(r){
            response.data+= (r.responseText.length);
            response.pending--; // decrement pending requests count
            response.cb(); // call the callback
        }});
    }(i));

    response.fordone = true; // mark the loop as done
    response.cb(); // call the callback
}()); 

This isn’t all very elegant but it does the job.
Is there any better way to do it? Perhaps a wrapper?

  • 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-04T15:34:23+00:00Added an answer on June 4, 2026 at 3:34 pm

    Async JS to the rescue (for both client-side and server-side JavaScript)! Your code may look like this (after including async.js):

    var datain = ['google','facebook','youtube','twitter'];
    
    var calls = [];
    
    $.each(datain, function(i, el) {
        calls.push( function(callback) {
            $.ajax({
                url : 'http://www.' + el +'.com',
                error : function(e) {
                    callback(e);
                },
                success : function(r){
                    callback(null, r);
                }
            });
        });
    });
    
    async.parallel(calls, function(err, result) {
       /* This function will be called when all calls finish the job! */
       /* err holds possible errors, while result is an array of all results */
    });
    

    By the way: async has some other really helpful functions.

    By the way 2: note the use of $.each.

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

Sidebar

Related Questions

I've been using Callable , but now I need the function to use a
I need a loop that waits for an async call before continuing. Something like:
Need to call a filter function on some options based on a radio selected
I need to call a library function that sometimes won't terminate within a given
I need to call a function in an unmanaged .dll written in C lang
I need to call from my code for some other program . And I
I need to call a c library from my python code. The c library
I need to call the co-class function by reading its address from vtable of
I'm writing unit tests using RhinoMocks for mocking, and now I'm in need of
I call a function that stacks two async calls and calls a callback when

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.