Robert
Robert

Reputation: 184

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?

Upvotes: 3

Views: 2770

Answers (1)

JB Nizet
JB Nizet

Reputation: 691735

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);
};

Upvotes: 6

Related Questions