Asynchronous code help

I realize that the code below if failing because message sets innerHTML before the second get returns, and I’ve read through a ton of articles dealing with asynchronous code, but am still unable to get it to work despite having spent a lot of time working on it.

If some kind soul has the patience to assist a relative noob, I could use some help in getting the below code working.

  $.get('/api/people', function(data) {
  var message="this many: ";

    $.each(data.Results, function(i, user){

      $.get("/api/people/"+user.Id+"/followers",function(dataFollowers) {
      message += dataFollowers.length+",";
    document.getElementById("GGAPI").innerHTML = message;

<div id="GGAPI">&nbsp;</div>

When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.


Read about the ajaxStop method to see a possible way to detect when all the API calls have completed.