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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:30:04+00:00 2026-05-28T20:30:04+00:00

First, this is a very specific case of doing it the wrong way on-purpose

  • 0

First, this is a very specific case of doing it the wrong way on-purpose to retrofit an asynchronous call into a very synchronous codebase that is many thousands of lines long and time doesn’t currently afford the ability to make the changes to “do it right.” It hurts every fiber of my being, but reality and ideals often do not mesh. I know this sucks.

OK, that out of the way, how do I make it so that I could:

function doSomething() {

  var data;

  function callBack(d) {
    data = d;
  }

  myAsynchronousCall(param1, callBack);

  // block here and return data when the callback is finished
  return data;
}

The examples (or lack thereof) all use libraries and/or compilers, both of which are not viable for this solution. I need a concrete example of how to make it block (e.g. NOT leave the doSomething function until the callback is called) WITHOUT freezing the UI. If such a thing is possible in JS.

  • 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-28T20:30:05+00:00Added an answer on May 28, 2026 at 8:30 pm

    “don’t tell me about how I should just do it “the right way” or whatever”

    OK. but you should really do it the right way… or whatever

    ” I need a concrete example of how to make it block … WITHOUT freezing the UI. If such a thing is possible in JS.”

    No, it is impossible to block the running JavaScript without blocking the UI.

    Given the lack of information, it’s tough to offer a solution, but one option may be to have the calling function do some polling to check a global variable, then have the callback set data to the global.

    function doSomething() {
    
          // callback sets the received data to a global var
      function callBack(d) {
          window.data = d;
      }
          // start the async
      myAsynchronousCall(param1, callBack);
    
    }
    
      // start the function
    doSomething();
    
      // make sure the global is clear
    window.data = null
    
      // start polling at an interval until the data is found at the global
    var intvl = setInterval(function() {
        if (window.data) { 
            clearInterval(intvl);
            console.log(data);
        }
    }, 100);
    

    All of this assumes that you can modify doSomething(). I don’t know if that’s in the cards.

    If it can be modified, then I don’t know why you wouldn’t just pass a callback to doSomething() to be called from the other callback, but I better stop before I get into trouble. 😉


    Oh, what the heck. You gave an example that suggests it can be done correctly, so I’m going to show that solution…

    function doSomething( func ) {
    
      function callBack(d) {
        func( d );
      }
    
      myAsynchronousCall(param1, callBack);
    
    }
    
    doSomething(function(data) {
        console.log(data);
    });
    

    Because your example includes a callback that is passed to the async call, the right way would be to pass a function to doSomething() to be invoked from the callback.

    Of course if that’s the only thing the callback is doing, you’d just pass func directly…

    myAsynchronousCall(param1, func);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of I'm very sorry but this questions is not so so specific. All
This is very basic magento question i guess. I want to first get all
This is my very first question so I am a bit nervous about it
First of all thanks in advance, this has been very frustrating and I'm hoping
So this was one of my very first questions here, but I have a
this is my first post here on stackoverflow and am very impressed by the
This may be a very mundane question, but this is the first jQuery plugin
I've been seeing this instruction as the very first line of numerous CSS files
Hiya I am very new to C#, infact this is basically my first task
Rather than giving the very specific case (which I did earlier), let me give

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.