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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:09:06+00:00 2026-06-08T06:09:06+00:00

lets say i have this async functuin: function fooAsync(){ callSomeApi(some_args, callbackFunction(results){ return results; //i

  • 0

lets say i have this async functuin:

function fooAsync(){
  callSomeApi(some_args, callbackFunction(results){
    return results; //i want to return the results here after the api call.
  }
}

i’m looking for a way to assign the return value from the prev function to a var and continue with the code only when i have this value.

//some code here
var foo = fooAsync();
//some code here after getting back from the async function.

the problem is that foo will be undefined since javascript will return before the inner async api call will finish. I know i can use callbacks for that but i’m looking for a way to ‘lock’ the async function and resume back only when it got the result. That way i wont have to pass all the code after the async call as a callback.

to make things short – how can i return value from async ajax call (in my case i call google maps api) in a regular sync way?

  • 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-08T06:09:07+00:00Added an answer on June 8, 2026 at 6:09 am

    Generally it’s a bad idea to make an ajax call synchronous. There is a property on the XMLHttpRequest object that you can set (jQuery allows you to do this easily) to make a synchronous ajax request. I’m not sure if the google maps API exposes this capability but you should check there first.

    I know you said you don’t want to work with callbacks but aside from doing something like this:

    while(foo === undefined){
      sleep(5) //pseudo code sleep method
    }
    

    there really isn’t any way to lock down the current execution context.

    Also with the code you provided, that method would never return anything other than undefined. You are calling the api method (which executes asynchronously) and immediately returning control. The ‘return’ statement inside of the response handler of the api method call would only return inside of the anonymous function. So even if you were able to lock the thread until the results are back, you wouldn’t have them.

    If, however, you are interested in going about this in the correct manner then you should use the deferred / promise model that jQuery provides.

    function fooAsync(){
      var deferred = $.Deferred();
      callSomeApi(some_args, callbackFunction(results){
        deferred.resolve(results) //i want to return the results here after the api call.
      }
      return deferred.promise();
    }
    

    Then you would change your calling code to be like this:

    //some code here
    $.when(fooAsync()).then(function(results){
        //use the results
    });
    //some code here after getting back from the async function.
    

    jQuery Deferred Object Documentation

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

Sidebar

Related Questions

Lets say i have this number: 1.5676556 But i want it to show only
Lets say i have this: $('a').each(function() { $(this).click(function(e) { e.preventDefault(); var href = $(this).attr('href');
Lets say I have this program below. Now I want to pass both the
Lets say I have this link here: https://svn.apache.org/repos/asf/chemistry/dotcmis/trunk/ and I would like to download
Lets say have this immutable record type: public class Record { public Record(int x,
Lets say I have this code: <?php class hello { var $greeting = hello;
Lets say i have this usercontrol public class test : UserControl { public int
Lets say I have this class in foobar-shared.lib: class FooBar { std::string m_helloWorld; }
Lets say we have this code: bool KeepGoing = true; DataInThread = new Thread(new
Lets say I have this code: public void MyMethod(string Data, List<string> InputData) { //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.