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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:08:33+00:00 2026-05-27T10:08:33+00:00

I have an application that uses several instances of getJSON, and I’m getting into

  • 0

I have an application that uses several instances of getJSON, and I’m getting into lots of trouble. Pointy once suggested reworking the main routine to include asynchronous processing, and I’m agreeing (now that I understand something of this).

Before attempting to rework this, it was structured like this:

Fill some arrays;
Call processArray to create a set of strings for each;
Stick the strings into the DIVs.

In the processArray routine, I call $.getJSON–a couple times, and you folks have pointed out that I’m getting into trouble with expecting values I have no right to expect. The overall routine processes an array into a complex string, but some arrays have to be sorted (unconventionally) first. My original structure began by asking: is this an array to be sorted? If so, I did such and such, involving getJSON, then returned to the main routine. What I had done to the array did not make it over the main routine, which continued to see the original array contents.

So, that processArray was configured like so:

 function processArray(arWorking, boolToSort...) {
if(boolToSort) {do special stuff}
//continue on with processing
return complexString;
} 

I figured that I would try to guarantee the inclusion of the sorted array in the main routine by replacing the ‘arWorking’ argument with a function that did the sorting if processArray was called with boolToSort = true. In my thinking, the rest of the main routine would go on with one of two forms of array: the original as passed or the sorted array. To this end, I made the sorting routine a separate routine: SortArray(arrayToUse).

I came up with this:

function processArray( function(arWorking) {if(boolToSort) SortArray(arWorking); else return arWorking;}, boolToSort, ...) {
//main routine
return complexString;
}

Both FireFox and IE9 object. FF breaks to jQuery, while IE9 wants an identifier in the calling arguments.
What looks to be wrong? Can I use boolToSort in my “argument function?”

  • 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-27T10:08:34+00:00Added an answer on May 27, 2026 at 10:08 am

    The first part of you understanding this is this:

    $.getJSON() does it’s work asynchronously. That means that when you call it, all it does is start the operation. The code following that function continues to execute while the $.getJSON() call works in the background. Some time later, the JSON results will be available and the success handler will get called.

    ONLY in the success handler can you use those results.

    As such, you cannot write normal procedural code that does this:

    function processArray() {
        $.getJSON(url, function(data) {
            // only in here can you process the data returns from the getJSON call
        })
        // you cannot use the JSON data here as it is not yet available
        // you cannot return any of the JSON data from the processArray function
    
    }
    

    Instead, you must write code that uses the success handler. Here’s one way of doing that:

    function processArrays(urlToProcess1, urlToProcess2, callbackWhenDone) {
        $.getJSON(urlToProcess1, function(data) {
            // only in here can you process the data returns from the getJSON call
            // do whatever you want to do with the JSON data here
            // when you are done process it, you can then make your next getJSON call
            $.getJSON(urlToProcess2, function(data) {
                // do whatever you want to do with the JSON data here
                // when done, you can then call your callback function to continue on with other work
                callbackWhenDone();
            });
        });
    }
    

    Another thing you cannot do is this:

    function processArray() {
        var result;
        $.getJSON(url, function(data) {
            // only in here can you process the data returns from the getJSON call
            result = data;
        })
        return(result);
    }
    
    var data = processArray();
    // code that uses data
    

    You cannot do this because the result data is not available when processArray() returns. That means not only can you not use it inside of processArray (but outside the success handler), but you cannot return it from processArray() and you cannot use it in code written after processArray(). You can only use that data from within the success handler or in code called from the success handler.

    If you had a whole bunch of URLs to process and you used the same code on each one, you could pass an array of URLs and loop through them, starting the next getJSON call only when the success handler of the first was called.

    If you had a whole bunch of URLs each with different code, you could pass an array of URLs and an array of callback functions (one for each URL).

    FYI, I see no issue with passing boolToSort. It sounds to me like the issue is with how you handle asynchronous ajax calls.

    For completeness, it is possible use synchronous ajax calls, but that is NOT recommended because it’s a bad user experience. It locks up the browser for the duration of the networking operations which is generally not a good thing. It’s much better to use the normal asynchronous ajax calls and structure your code to work properly with them.

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

Sidebar

Related Questions

I have an application that uses a SQL Server database with several instances of
I am building an android application that uses several third party libraries. I have
I have an application that uses AJAX liberally. I have several places where a
I have an application that uses MVVM. I have several items on the main
I have an ASP.NET MVC application that uses jQuery 1.3.2. On several of the
We have an application that uses several out of process COM objects for various
I have a spring application that uses several services such as MySQL, PostgreSQL, and
I have an application that uses cURL to grab the contents of several websites.
I have a REST application that uses RESTlets. I am querying several databases to
I have a small wxWidget application that uses image files at several places. I

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.