0

Is there any way of adding a callback method to a function that calls service in a function i am calling multiple service methods and i want to a set a callback method to that function. I tried this but no luck

$scope.MyFunction() = function()
{
    //Multiple Service calls
    $scope.callService1 = service.CallService1() //returns true on success
    $scope.callService2 = service.CallService2() //returns true on success
    $scope.callService3= service.CallService3() //returns true on success

    if($scope.callService1  && $scope.callService2 && $scope.callService3)
    {
        $scope.CallbackMethod ();
    }
}

$scope.CallbackMethod = function()
{
    alert('CallbackMethod')
}

I tried this one too but its not in sync as the service calls takes some time.

$scope.MyFunction() = function(CallbackMethod)
{
    //Refer Above Code
} 

CallService service Method is something simple like

$http.post('/InstStrategy/ReadAll').then(function (response) {
    return true
});
4
  • Sounds like you're looking for promises docs.angularjs.org/api/ng/service/$q Commented Oct 23, 2015 at 17:14
  • Show us the content of CallService1 function, please Commented Oct 23, 2015 at 17:15
  • Edited the question with the service function. Thanks Commented Oct 23, 2015 at 17:29
  • Your service method doesn't return anything. So callService1 (and 2 and 3) are all undefined. Commented Oct 23, 2015 at 18:16

1 Answer 1

2

Assuming that CallService1, CallService2 and CallService3 are async methods, they should return a deferred promise. For example:

this.CallService1 = function() {
  // Once the result is available resolve the promise.
  return $http.post('/InstStrategy/ReadAll').then(function(response) {
    return true;
  });
}

Now, it's time to define your function:

$scope.MyFunction = function(callback) {
  var callService1 = service.CallService1(),
      callService2 = service.CallService2(),
      callService3 = service.CallService3();

  // We want to wait for all these three methods to complete.
  $q.all([callService1, callService2, callService3])
    .then(function(results)) {
          // Results is an array containing the results of each of your service calls.
          var allTrue = true;
          angular.forEach(results, function(result) {
            if (!result) allTrue = false;
          });

          // If all the service calls where true, perform our callback.
          if (allTrue) callback();
    });
};

Note that the allTrue check is not really necessary, since the promises are systematically resolved with true.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you update your first answer without using the "forgotten promise" pattern? Your answer would be read by many people so it's in their best interest to read the best practices.
I edited your answer to make it return a rejected promise in case the $http call fails. Your original answer used an antipattern, as noted by lycool. Read blog.ninja-squad.com/2015/05/28/angularjs-promises

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.