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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:01:39+00:00 2026-06-02T21:01:39+00:00

Here’s a description of my problem: i have an array containing ids for some

  • 0

Here’s a description of my problem:

i have an array containing ids for some flickr photosets.
for each id, i need to make two nested apiCalls (one to flickr photosets api, one to photoinfo api) and add some html to my page

the structure is:

for(var i=0; i<ARRAY_ID.length; i++){

    $.getJSON(apiCall, function(data) {
         // some processing here

         $.getJSON(apiCall2,function(data){
              //other processing here

each “apiCall” is a string containing the correct call to the photosets api (apiCall) and then for the photoInfo api (apiCall2)

after all this processing, inside the “apiCall2” block, i append some html to a div (which results in as many block elements as the ARRAY_ID length).

There’s where i have the strange behaviour: all appended block elems contain the same text and picture and links. It prints them out in the expected number, but the content is the same throu all of them. I guess it’s the for loop not waiting for the $.getJSON to finish. How can i achieve this? I strictly need each json request to be finished before the next one is processed! Thanks all!

  • 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-02T21:01:41+00:00Added an answer on June 2, 2026 at 9:01 pm

    You seem to have 2 different, but related, issues going here.


    all appended block elems contain the same text and picture and links. It prints them out in the expected number, but the content is the same throu all of them.

    If you’re referencing i within the callbacks, you’ll need to enclose it within the for. Otherwise, with $.getJSON being asynchronous, the for will finish before the callbacks are called and i will always be the last value in the loop — ARRAY_ID.length:

    for(var i = 0; i < ARRAY_ID.length; i++) {
        (function (i) {
            // work with the local `i` rather than the shared `i` from the loop
        })(i);
    }
    

    I guess it’s the for loop not waiting for the $.getJSON to finish. How can i achieve this? I strictly need each json request to be finished before the next one is processed!

    You can’t get the for to “wait” for each set of $.getJSON to finish. You can, however, use the for loop and closures to build a queue, which waits until you explicitly tell it next to continue:

    var focus = $(...); // either match an existing element or create one: '<div />'
    
    for(var i = 0; i < ARRAY_ID.length; i++) {
        (function (i) {
            focus.queue('apicalls', function (next) {
                $.getJSON(apiCall, function(data) {
                     // some processing here
    
                     $.getJSON(apiCall2,function(data){
                          //other processing here
    
                          next(); // start the next set of api calls
                     });
                });
            });
        })(i);
    }
    
    focus.dequeue('apicalls');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my problem : I have a post controller with the action create.
Here is my code...I have two dimensional matrices A,B. I want to develop the
Here's a problem I ran into recently. I have attributes strings of the form
Here is my problem...I have a page that loads a list of clients and
Here is an example: I have a file 1.js, which has some functions. I
Here is the skeleton of some basic code I am writing to make a
Here's my proposed (very simplified to illustrate the problem space) design for a C#
Here is some simple code: DIR* pd = opendir(xxxx); struct dirent *cur; while (cur
I have just tried to save a simple *.rtf file with some websites and
Here's the basic setup: I have a thin bar at the top of a

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.