How to catch asynchronous result


#1

Hi,
i have this helper:
distanceIs: function (destination){
console.log(‘distanceIs helper’);
console.log(destination);
var origin = Session.get(‘userLatLng’);
console.log(origin);
var isDistance = Blaze._globalHelpers.getDistance(origin, [destination[0], destination[1]] );
console.log(isDistance); //
//Tracker.autorun(function() {
if (isDistance) // returns undefined because it returns after it’s executed…
return isDistance;
// })
}
I tried with the Tracker.autorun and Meteor.defer, but in both cases with no results…
Thanks.


#2

Can we see your getDistance helper?

BTW: If you wrap your code between triple backticks, it will be easier for us to read:

```
like
this
```

#3

Hi,
I have updated the code now and all is working except for the fact that the html is not getting updated:

distanceIs: function (destination, id){
var origin = Session.get(‘userLatLng’);
Tracker.autorun(function(){
console.log(Blaze._globalHelpers.getDistance(Template.instance().distances, id, origin, destination)); /OK

        return Blaze._globalHelpers.getDistance(Template.instance().distances, id, origin, destination);
    });
}

Template.registerHelper(‘getDistance’, function(template, id, origin, destination){
var map;
var directionsDisplay;
var directionsService;
directionsDisplay = new google.maps.DirectionsRenderer;
directionsService = new google.maps.DirectionsService;
directionsDisplay.setMap(map);
directionsService.route({
origin: {lat: origin[0], lng: origin[1]},
destination: {lat: destination[1], lng: destination[0]},
travelMode: google.maps.TravelMode[“DRIVING”]
}, function (response, status) {
if(status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
directionsDisplay.setOptions({suppressMarkers: true});
var distanceinmeter = (response.routes[0].legs[0].distance.value);
var timeInMinutes = response.routes[0].legs[0].duration.value;
console.log(distanceinmeter); //OK
template.set(id, distanceinmeter );
Tracker.autorun(function(){
console.log(template.get(id));
return template.get(id);
});
} else {
console.log(‘rout not found’);
}
})
});