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

  • Home
  • SEARCH
  • 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 8164523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:20:10+00:00 2026-06-06T19:20:10+00:00

I have a for loop in a search function, with a function that does

  • 0

I have a for loop in a search function, with a function that does a callback inside the loop, and I want to execute a BUILD() function after the loop, and after all the callbacks are completed. I am not sure how to do that, because the loop finishes before all the callbacks are done. The callbacks are API requests to get me data, and I want to BUILD() with that data.

I read up on deferred, so I tried to put the for loop inside a function to the deferred, and then calling BUILD() on ‘.then( … )’. But that doesn’t seem to work – I think I am understanding it wrong.

HELP?!

Note, this is using the Google Maps Places API (search and getDetails).

var types = {
    'gym' : 'fitness, gym',
    'grocery_or_supermarket': ''
}

function search() {
    for (var key in types) {
         var request = { ... };
         service.search(request, searchCallback);
    }
    // PROBLEM AREA
    BUILD();
}

function searchCallback(results, status) {
    for (var i = 0; i < results.length; i++) {
        var request = { ... };
        service.getDetails(request, detailsCallback);
    }
}

function detailsCallback(place, status) {
    // add place marker to maps and assign info window and info window event
}
  • 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-06T19:20:11+00:00Added an answer on June 6, 2026 at 7:20 pm

    With a small modification of your code, it can be achieved.

    var total = 1337; // Some number
    var internal_counter = 0;
    var fn_callback = function() {
        searchCallback.apply(this, arguments);
        if (++internal_counter === total) {
            BUILD();
        }
    };
    for (var i=0; i<total; i++) {
        service.search(request, fn_callback);
        ...
    

    Explanation

    First, we create a local function and variable.

    • The variable is a counter, which is increased when the callback is called.
    • The function is passed to the asynchronous method (service.search), which calls the original callback. After increasing the counter, check the value of the counter against the variable which holds the total number of iterations. If these are equal, call the finishing function (BUILD).

    A complex case: Dealing with nested callbacks.

    var types = { '...' : ' ... ' };
    
    function search() {
        var keys = Object.keys(types);
        var total = keys.length;
        // This counter keeps track of the number of completely finished callbacks
        //  (search_callback has run AND all of its details_callbacks has run)
        var internal_counter = 0;
    
        for (var i=0; i<total; i++) {
            var request = { '...' : ' ... ' };
            services.search(request, fn_searchCallback);
        }
    
        // LOCAL Function declaration (which references `internal_counter`)
        function fn_searchCallback(results, status) {
            // Create a local counter for the callbacks
            // I'm showing another way of using a counter: The opposite way
            // Instead of counting the # of finished callbacks, count the number
            //  of *pending* processes. When this counter reaches zero, we're done.
            var local_counter = results.length;
            for (var i=0; i<results.length; i++) {
                service.getDetails(request, fn_detailsCallback);
            }
            // Another LOCAL function (which references `local_counter`)
            function fn_detailsCallback(result, status) {
    
                // Run the function logic of detailsCallback (from the question)
                // " ... add place marker to maps and assign info window ... "
    
                // Reduce the counter of pending detailsCallback calls.
                //   If it's zero, all detailsCallbacks has run.
                if (--local_counter === 0) {
                    // Increase the "completely finished" counter
                    //  and check if we're finished.
                    if (++internal_counter === total) {
                        BUILD();
                    }
                }
            } // end of fn_detailsCallback
        } // end of fn_searchCallback
    }
    

    The function logic is explained in the comments. I prefixed the heading of this section with “Complex”, because the function makes use of nested local functions and variables. A visual explanation:

    var types, BUILD;
    function search
        var keys, total, internal_counter, fn_searchCallback;
        function fn_searchCallback
            var result, status; // Declared in the formal arguments
            var local_counter, i, fn_detailsCallback;
            function fn_detailsCallback
                var result, status; // Declared in the formal arguments
    

    In the previous picture, each indention level means a new scope Explanaation on MDN.
    When a function is called, say, 42 times, then 42 new local scopes are created, which share the same parent scope. Within a scope, declared variables are not visible to the parent scope. Though variables in the parent scope can be read and updated by variables in the “child” scope, provided that you don’t declare a variable with the same name. This feature is used in my answer’s function.

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

Sidebar

Related Questions

I have a Google Chrome extension that creates a select-all function for a search
I have some enums, all different, and I want to create a function that
I have a process as follows: User does a complex search that is done
I have a function that is meant to clear a div of all its
I have a search function that returns an arraylist of objects to my form.
I have a pagination function that I use for my database search that limits
I have a binary search loop which gets hit many times in the execution
I have a loop that enters each non-empty text field into a database: foreach
I have a loop $(.box).find('.video, .print, .web').closest('.box').each(function (index) { $(this).delay(100 * index).fadeIn(300); }); This
I have created my application that has search form and below contains results for

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.