Skip to content Skip to sidebar Skip to footer

Issue With Nested Asynchronous Function Not Returning Data In Time

I rely on a component i.e. Angular Material Autocomplete that requires a function that returns a value. Unfortunately, I am not sure how to return something in due time from the ne

Solution 1:

You need to expose the fact that the call is asynchronous to the callees of chooseAddress. You can achieve this by returning a promise.

Update the implementation to

$scope.chooseAddress = function (input) {
    var deferred = $q.defer();

    if (input) {
        geolocationService.addressAutocomplete(input, function (data) {
            deferred.resolve(data.predictions);
        });
    } else {
        deferred.resolve([]);
    }
    return deferred.promise;
};

You then call chooseAddress like

$scope.chooseAddress(input).then(function(result){
    // the result will be available here
});

Post a Comment for "Issue With Nested Asynchronous Function Not Returning Data In Time"