Using Global var to cycle through AJAX doesn't work

Using Global var to cycle through AJAX doesn't work

I am trying to call AJAX three maybe four times to get information from my DB. Can't get "global" var mapAddress
to return. I've tried to position the function in various locations but can't get it right. I'm sure this has to do with asynchronous functioning but still don't understand it.

Thanks for help,

R
<script type="text/javascript">

  1. var recId="";
    var mapAddress="";      // declaring here to create global var


    // ==================================================================
    // ===========  lots of other code including code
    // =========== that triggers $('.map').click function below =========
    // ==================================================================

    $('.map').click(function () {

    // ======== there may be two addresses to process =========
    // ======== home address and work address ==============

    var phaseFlag=1;

  2. // MULTIPLE CALL TO AJAX

  3. // first phase query the DB to SELECT all address, city, state data ======
    // phase two would be a call to SELECT home address info =================
    // phase three would be a call to SELECT work address info  ==============


    mapAddress=myAjaxCallGeo(phaseFlag);

    //  =========== do stuff with mapAddress ===============

    alert("ajax "+mapAddress); // mapAddress is undefined

    });

    </script>

    <script type="text/javascript">

    function myAjaxCallGeo(phaseFlag) {

    var request = $.ajax({
        url: "getLatLong2.php",
        type: "GET",
        data: {"id":choice, "phase":phaseFlag},
        dataType: "json"        
    });

    request.done(myData );
           
    request.fail(function( jqXHR, textStatus ) {
      alert("Line 279 - Bad request, see firebug");
           
    });

     function myData(address) {
         var addFlag="";
        
         if (address) {
             if (address.Home_Address) {
                 addFlag="hm";
                 mapAddress=addFlag+", "+address.Home_Address+" "+address.Home_City+", "+address.Home_State;   
             }
             else if(address.Work_Address) {
                 addFlag="wrk";
                 mapAddress=addFlag+", "+address.Work_Address+" "+address.Work_City+", "+address.Work_State;            
             }
           

         }

    }
           
    }// myAjaxCall

    </script>