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

  • Home
  • SEARCH
  • 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 8184251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:26:20+00:00 2026-06-07T01:26:20+00:00

I for my life cannot figure this out. I know it’s some issue with

  • 0

I for my life cannot figure this out. I know it’s some issue with the asynchronous way ajax makes calls but still cannot pin point the issue. I have this:

$(document).ready(function() {
  $('#address').blur(function() {
    console.log("now processing address");
    var add = $("#address").val();
    console.log("address is: " + add);
    var refAdd = processAddress(add);
    console.log("refined addres is: " + refAdd);
  }); 
});

And then I have the processAddress function (thanks to another post on SO) which I’m calling. The issue is that the last statement above returns refAdd as undefined. Why is that??

function processAddress(address){
  var geocoder = new google.maps.Geocoder();
  if (geocoder) {
    geocoder.geocode({ 'address': address }, function (results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        //console.log(results[0].geometry.location);
        console.log(results[0].formatted_address);
        console.log("latitude is: " + results[0].geometry.location_type);
      }
      else {
        console.log("Geocoding failed: " + status);
        //formatted_address = "Geocoding failed: " + status;
        formatted_address = "failed"
      }
    });
  } 
}

It’s pretty clear is a asynchronous issue, I can see in the order of how output is printed; refAdd should be printed last but it’s not.

now processing address
address is: 415 N Mathilda Ave, Sunnyvale
refined addres is: undefined
415 N. Mathilda Ave, Sunnyvale, CA, USA
latitude is: ROOFTOP
  • 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-07T01:26:22+00:00Added an answer on June 7, 2026 at 1:26 am

    You are right, the problem is because the function is asynchronous (how do I know that? because as you said: logs are written asynchronously and geocoder.geocode uses a callback). You have to use callbacks, like this:

    $(document).ready(function() {
      $('#address').blur(function() {
        console.log("now processing address");
        var add = $("#address").val();
        console.log("address is: " + add);
    
        // adding callback to call.
        processAddress(add, function(refAdd) {
          console.log("refined addres is: " + refAdd);
        });
      }); 
    });
    

    and in the function:

    function processAddress(address, callback){ // <---- note the callback arg
      var geocoder = new google.maps.Geocoder();
      if (geocoder) {
        geocoder.geocode({ 'address': address }, function (results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            //console.log(results[0].geometry.location);
            console.log(results[0].formatted_address);
            console.log("latitude is: " + results[0].geometry.location_type);
            callback('TEST'); // <---- pass here the value you need
          }
          else {
            console.log("Geocoding failed: " + status);
            //formatted_address = "Geocoding failed: " + status;
            formatted_address = "failed"
            callback('TEST'); // <---- pass here the value you need
          }
        });
      } else {
        callback(); // <---- pass here for example error if you need
      }
    };
    

    I do not know this geocoder stuff, so you need to customize this to your own needs.

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

Sidebar

Related Questions

spent a few hours trying to figure this out, but cannot for the life
For the life of me, I cannot figure out why this NSTimer wont fire.
I cannot for the life of me figure out why this script is not
For the life of me I cannot figure out why I can’t get this
I cannot figure this out for the life of me. I need to have
For the life of me, I cannot figure out why the code at www.moosecodes.com
I cannot for the life of me figure out why my table is turning
For the life of me, I can not figure out why some styles are
I have a slight problem and cannot for the life of me figure out
For the life of me, I cannot figure out why the below statement in

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.