AngularJS promise chain

rascio picture rascio · Aug 28, 2013 · Viewed 9.3k times · Source

I have my application that should open a popup ask a confirmation at the user, then make an ajax cal and close the popup.
I tried to do it using a chain of promise (I've already used it, and I remember that it should work in this way), but it seems to block after the call toreservationService.confirm($scope.object);. Now it is a fake service implemented with a setTimeout and $q just to return a promise (in future it will make the ajax call). Is this a valid code or I didn't undestood how promise works?
For the popup I choose AngularUI and the code is this:

 reservationService.book($scope.object, day)
        .then(function(){
            var dialogOpts = {/* dialog options omitted*/}
            return $dialog.dialog(dialogOpts).open();

        })
        .then(function(result){
            console.log('confirmed? ', result);
            if (result){
                //After this line it doesn't do nothing, also if the promise is resolved 
                return reservationService.confirm($scope.object);
            }
        })
        .then(function(){
            //this function is never executed
            $scope.$emit('object:detail',{object: $scope.object});
        });

reservationService:

function confirm(){
     var deferred = $q.defer();
     setTimeout(function(){
          console.log('Confirming');
          deferred.resolve(true)
     }, 500);
     return deferred.promise;
}

SOLVED change setTimeout with $timeout angular's service

Answer

rascio picture rascio · Sep 3, 2013

Used $timeout instead of setTimeout 'cause it works togheter at the angular scope, forcing the digest phase (or use $scope.apply() inside the setTimeout).