AngularJS promise chain

限于喜欢 提交于 2019-12-05 11:44:05

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

Can you try

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

For chaining then, the last then success or failure function should return a promise. As the $q documentation mentions

then(successCallback, errorCallback) – regardless of when the promise was or will be resolved or rejected, then calls one of the success or error callbacks asynchronously as soon as the result is available. The callbacks are called with a single argument: the result or rejection reason.

This method returns a new promise which is resolved or rejected via the return value of the successCallback or errorCallback.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!