0

As the title says, even though it may be a little bit vague, when i call a function which returns an array of objects in another function, for some reason i'm not able to catch the result in another object in the current function. I have a feeling that is just a little thing that i'm missing.

Here is the function which returns the array of objects :

function GetAllNeighboors(myLocationLat, myLocationLong) {
    $.get("/Home/GetAllLocation",
        function (data, status) {
            Neightbours = new Object();
            var j = 0;
            for (var i = 0; i < data.length; i++) {
                if (distanceBetweenTwoStations(myLocationLat, myLocationLong, data[i].latitudine, data[i].longitudine,"K") <= 1 && data[i].latitudine != myLocationLat && data[i].longitudine != myLocationLong) {
                    Neightbours[j] = data[i];
                    j++;
                }
            }
            return Neightbours;        
        }); 

}

And here is where i'm calling that function:

if (data.isClosed != true) {
                    map.setCenter(new google.maps.LatLng(data.latitudine, data.longitudine));
                    map.setZoom(17);
                } else {
                    Vecini = new Object();
                    Vecini = GetAllNeighboors(data.latitudine, data.longitudine);

NOTE: The Neightbours from the first function comes with the expected result.

NOTE2: I think i figured out what is the problem, but i'm not really sure how i can solve it. The problem is i'm not waiting for the result, i should make the function asynchronous, in order to have the result in the second function. Any idea how i can do that?

seceneu
  • 23
  • 4
  • 1
    Possible duplicate of [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) – melpomene May 18 '19 at 15:31

2 Answers2

0

You arent returning anything from the first function, you are returning from the function inside of that function. Try this:

function GetAllNeighboors(myLocationLat, myLocationLong) {
   return $.get("/Home/GetAllLocation",
        function (data, status) {
            Neightbours = new Object();
            var j = 0;
            for (var i = 0; i < data.length; i++) {
                if (distanceBetweenTwoStations(myLocationLat, myLocationLong, data[i].latitudine, data[i].longitudine,"K") <= 1 && data[i].latitudine != myLocationLat && data[i].longitudine != myLocationLong) {
                    Neightbours[j] = data[i];
                    j++;
                }
            }
            return Neightbours;        
        }); 

}
aprouja1
  • 1,800
  • 8
  • 17
0

You can pass a callback to your GetAllNeighbours function and set its value inside the $.get callback like this

function GetAllNeighboors(myLocationLat, myLocationLong, cb) {
    $.get("/Home/GetAllLocation",
        function (data, status) {
            Neightbours = new Object();
            var j = 0;
            for (var i = 0; i < data.length; i++) {
                if (distanceBetweenTwoStations(myLocationLat, myLocationLong, data[i].latitudine, data[i].longitudine,"K") <= 1 && data[i].latitudine != myLocationLat && data[i].longitudine != myLocationLong) {
                    Neightbours[j] = data[i];
                    j++;
                }
            }
            cb(Neightbours);        
        });
}

And you now call it like this:

if (data.isClosed != true) {
   map.setCenter(new google.maps.LatLng(data.latitudine, data.longitudine));
   map.setZoom(17);
} else {                    
   GetAllNeighboors(data.latitudine, data.longitudine, function(Neighboors){
       Vecini = new Object();
       Vecini = Neighboors
});

Ramon Portela
  • 419
  • 2
  • 8
  • It worked, it finally worked. And if i want to do something with `Vecini` i have to do it under `GetAllNeighboors` function, right? – seceneu May 19 '19 at 16:20
  • Yes, you'll have to do it inside the callback function where the value of ```Vecini``` is being set GetAllNeighboors(data.latitudine, data.longitudine, function(Neighboors){ Vecini = new Object(); Vecini = Neighboors; console.log(Vencini); }); – Ramon Portela May 19 '19 at 16:23