-1

I am calling function from ajax success and trying to return value from the function

ajax success is

  success: function(data)
  { 
    var distance_arr;    
    data = JSON.parse(data);
    for (var i = 0; i < data.length; i++) 
    { 
        if(data[i].port_name != destination)
        {
            distance_arr = get_port_distance(data[i].port_name,source,type);
            alert(distance_arr);
        }

    }
  }

In get_port_distance function I am returning value as

    return  distance; 

by calculating distance in get_port_distance function. When I alert distance in get_port_distance then it is correct but when I return it It alert blank in ajax success function

function get_port_distance(source,destination,type) 
{ 
    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();


    var request = 
    {
        origin: source,
        destination: destination,
        travelMode: google.maps.TravelMode.DRIVING
    };

        var service = new google.maps.DistanceMatrixService();
        service.getDistanceMatrix({
        origins: [source],
        destinations: [destination],
        travelMode: google.maps.TravelMode.DRIVING,
        unitSystem: google.maps.UnitSystem.METRIC,
        avoidHighways: false,
        avoidTolls: false
    }, function (response, status) {
    if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") {
        var port_distance = response.rows[0].elements[0].distance.text;
        port_distance = port_distance.replace(/\,/g,'').replace(' km',''); // To REMOVE comma(,)
        return  port_distance;

        } 
    });
}    
hrishi
  • 1,610
  • 6
  • 26
  • 43
  • can you please share the code for get_port_distance too as. – Sumit Surana Nov 28 '16 at 05:48
  • Your looping condition might be giving you blank value. , you might want to break the loop when you get distance value from function – Afsar Nov 28 '16 at 05:48
  • If you think the problem is in `get_port_distance` function you should post it – RizkiDPrast Nov 28 '16 at 05:48
  • 1
    `if(data[i].port_name != destination)` Where `destination`came from, I dont see this variable in your code, also, there is `source`, and `type`. Paste full code for ajax and `get_port_distance`. – Aleksandar Nov 28 '16 at 05:49
  • source, type and destination I get it in the function where I called success. I pass them to other function for calculations. get_port_distance function is too large to post. But I get output correct there. But When I return value it dont get as expected – hrishi Nov 28 '16 at 05:54
  • 1
    Does `get_port_distance()` do anything asynchronous? – nnnnnn Nov 28 '16 at 06:01
  • You must paste this function somewhere, like jsfiddle.net. We can't guess where error occurs. Maybe the problem is in some of this variables or other small thing, but without viewing the full source, we can't help you. – Aleksandar Nov 28 '16 at 06:04
  • I have posted get_port_distance() in question with required code – hrishi Nov 28 '16 at 06:06
  • 5
    [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) In your case, `service.getDistanceMatrix()` is asynchronous and that forces `get_port_distance()` to need to be written to work asynchronously as well (in short, that means `return port_distance;` isn't an option). – Jonathan Lonowski Nov 28 '16 at 06:17

1 Answers1

1

return port_distance;

is placed inside:

function (response, status) {}.

That means that you are not returning anything from:

function get_port_distance(source,destination,type){}

See this Stackflow answer for help solving the problem returning from an async function.

Community
  • 1
  • 1
rabbitco
  • 2,790
  • 3
  • 16
  • 38
  • Yes. But how I can return it. Now I returned it outside function (response, status) {} it gives error port_distance is not defined – hrishi Nov 28 '16 at 06:18
  • Updated answer - see link – rabbitco Nov 28 '16 at 06:23
  • *Yes. But how I can return it.* You cannot return a future or asynchronous value--it doesn't exist yet. –  Nov 28 '16 at 07:21
  • I checked link I am not much familiar with java script. It is hard to understand explanation in the link for me for my code – hrishi Nov 28 '16 at 07:27