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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:05:34+00:00 2026-05-28T01:05:34+00:00

I am trying to update array values using the response from multiple getJSON requests.

  • 0

I am trying to update array values using the response from multiple getJSON requests. I have tried this a number of ways and the snippet below is the simplest form I have used. The individual success callbacks have not updated the array by the time the .then() fires so the alert shows the original values. I have also tried to store the individual jqXHR objects in an array and work through them, but they are all readyState: 1. Any suggestions on how to get a reference to the responses after they have all completed?

$(function() {

    var points = [['A', 12946],
                  ['B', 4223],
                  ['C', 2774],
                 ];

    function makePointCalls(i) {
        return $.getJSON("http://otter.topsy.com/searchcount.json?callback=?",
            {
                q: points[i][0]
            },
            function(data) {
                points[i][1] = data.response.d;
            }
        );
    }

    $.when($.each(points, function(i, value){
              makePointCalls(i);
            })
        ).then(function(){
            //alert me after all the getJSONs are done and array updated?
            alert(points);
        });
});//doc ready
  • 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-28T01:05:34+00:00Added an answer on May 28, 2026 at 1:05 am

    Your code needs to react on the return value of makePointCalls which is discarded in your each loop.

    Untested code fragments follow (working according to kas673’s comment):

    // Collect deferreds first
    var deferreds = jQuery.map(points, function(i, value){
        return makePointCalls(i);
    });
    
    // Pass all deferreds to when
    jQuery.when.apply(jQuery, deferreds).then(…);
    

    Note the use of map instead of each to create an array with the return values (deferreds) of makePointCalls. The call to apply for invoking when is there to ensure when receives its parameters in the documented format. If you would pass an array to when as sole argument it could just assume it received an resolved deferred and execute the function in then immediately.

    A more verbose explanation regarding the use of apply is that when assumes everything is a resolved deferred that is not a deferred. First of all one needs to note that a deferred is some sort of callback manager. It holds callbacks to invoke on success/resolve and callbacks for failure/rejection. Deferreds are used to provide a return value for which it is only known at a later time whether the function invocation was successful. An example is your AJAX request that needs to wait for the server but should not block the JavaScript execution.

    Each of your calls to $.getJSON returns a deferred which can be used to react on the requests completion. $.when takes any number of deferreds as its arguments and invokes then‘s argument as soon as the aggregate state of the deferreds is known (either when all arguments to when are resolved or at least one was rejected).

    Calling $.when(deferreds).then(callback) leads to immediate invocation of callback as mentioned before when deferreds as an array of Deferred objects. Calling $.when(deferred[0], deferred[1]).then(callback) does invoke callback when the aggregate state is known. Using apply allows you to unpack the array and use each element in the array as a parameter to the function on which apply is invoked (that’s when in your case).

    Check your ActiveX notifications again since Firebug runs in Firefox where ActiveX is fortunately not supported. Anything related to ActiveX there is probably because you are using a Microsoft-only API which you should not do.

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

Sidebar

Related Questions

I'm trying to make an array, and then update the values of certain cells
This is the code giving me issue - I'm trying to update multiple records
I am trying to update a field in a table with data from another
So I'm trying to update an object in my MS SQL 2005 database using
I'm trying to update an NText field in SQL 2000 using Classic ASP. Here
Hi I am trying to update a div with some html from another div.
I am trying to loop through an integer array using C# Iterators. The program
Bash 4 on Linux ~ I have an array of possible values. I must
I have an array of values being made available, but unfortunately some of the
I'm trying to use saveAll correctly to update User records for multiple relational models.

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.