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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:39:04+00:00 2026-06-17T14:39:04+00:00

I have a function which takes a parameter and does synchronous loadings. I changed

  • 0

I have a function which takes a parameter and does synchronous loadings.

I changed it to do an asynchronous jQuery.ajax call to retrieve data and call a callback. Old code (which cannot be modified) uses the same function and fails. I need to be able to retrieve the data both ways so async code can keep on running and old sync code can retrieve the cached data if async already loaded it, load it synchronously or wait for any async requests which are already running.

var ajaxQueries = {}
loadEngineData(fileId, callback) {
    var sync = callback == undefined;
    if (ajaxQueries[fileId]) {
        if (sync) {
            //Need to stop here and wait for ajaxQueries[fileId] to finish
            //Execution cannot continue! If we return before defered has ended here 3rd party scripts will fail.
            return data;
        }
        else {
            ajaxQueries[fileId].done(function(data){callback(data)});
        }
    }
    else {
        ajaxQueries[fileId] = $.ajax({
            async:!sync,
            type:'GET',
            url:fileId2Name(fileId),
            data:null,
            dataType:'text',
        });
        if (sync) {
            var _data = undefined;
            ajaxQueries[fileId].done(function(data){_data=data});
            return _data;
        }
        ajaxQueries[fileId].done(function(data){callback(data);});
    }
}

If an old script calls the function the ajax query will run synchronously and all subsequent calls will use the deferred .done and .fail and everything will be fine.

var enginePhyData = loadEngineData('physics');
//Do stuff

If a new script calls the function first and during this loading an old script tries to load data from the same file, I end with a situation where the deferred ajax query in ajaxQueries[fileId] is running asynchronously and I have to wait for it to finish loading the data to avoid breaking the existing libs.

I simplified loadEngineData to keep the question focussed. Otherwise it’s a bit more complex and allows calling a list of files to load in parallel

loadEngineData(['geometry', 'scripts'], function() {
    var phy = loadEngineData('physics');
    var geo = loadEngineData('geometry');
    var scr = loadEngineData('scripts');

    //run the async function;
});
loadEngineData('physics', function() {
    //run the async function;
});

//...
//in the scripts file

var phy = loadEngineData('physics');
//here phy might be undefined if this file went through eval() before `loadEngineData('physics',...)` was called;

I could modernize the code, but the issue is that some files are shared across projects and run in different environments. I can’t currently modify the old scripts without forking the dependencies.

  • 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-17T14:39:06+00:00Added an answer on June 17, 2026 at 2:39 pm

    You cannot stop the execution of javascript while waiting for some asynchronous ajax call to finish. Javascript simply doesn’t work that way.

    Instead, you have to rework the way you program so that code that wants to wait for an asychronous ajax call to be completed will execute via a callback function, not synchronously after the ajax call executes.

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

Sidebar

Related Questions

I have a variadic function which takes a float parameter. Why doesn't it work?
I have a function which takes a block of data and the size of
I have a function which takes a String list as parameter which, at the
I have a generic JavaScript function which takes one parameter function foo(val) { ...}
I have a function which takes in two parameters, and returns one or the
I have a function which takes two arguments, the ID of an item (wine)
I have a function which takes several boolean template arguments: template<bool par1, bool par2,
I have a function which takes a custom struct as the argument. how can
The title more or less says it all: I have a function which takes
I have a merge function which takes time O(log n) to combine two trees

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.