Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make angular wait for a function to return a value before proceeding

I have function like this:

$scope.process = function(){
     $http().success(){
         return something;   
     }
};

Just assume that the code is complete.. and then when I call it

alert($scope.process());

it displays undefined.

How do I get the angular to wait for the return of the function before proceeding?

like image 209
Robert Avatar asked Sep 10 '26 21:09

Robert


1 Answers

You don't. Instead, you tell it what to do when the response is available:

$scope.process = function() {
    $http.get(path).success(function(data) {
        alert("I just received " + data);
    });
};

Or, if you want this to be configurable:

$scope.process = function(callback) {
    $http.get(path).success(callback);
};
like image 118
JB Nizet Avatar answered Sep 13 '26 10:09

JB Nizet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!