Denter
Denter

Reputation: 3

Resolving a promise in a 'then' statement

With AngularJS, I resolve my promises directly in the service just like this:

.factory('movieService', function($http, $log, $q) {
  return {
   getMovie: function(movie) {
     var deferred = $q.defer();
     $http.get('/api/v1/movies/' + movie)
       .success(function(data) { 
          deferred.resolve({
             title: data.title,
             cost: data.price});
       }).error(function(msg, code) {
          deferred.reject(msg);
          $log.error(msg, code);
       });
     return deferred.promise;
   }
  }
 });

As stated in the documentation (https://docs.angularjs.org/api/ng/service/$http#) :

The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error.

So success and error got deprecated.

How can I resolve a promise in a then statement ?

Regards.

Upvotes: 0

Views: 483

Answers (3)

Harris
Harris

Reputation: 1785

Just pass two functions to then() as parameters, the first being for success, and the second being for failure.

...

$http.get('/api/v1/movies/' + movie)
.then(function(result) { 
          //Your success code here
       },
      function(result) {
          //Your fail code here
       });
...

Strictly speaking, though, then() returns a promise. What you're doing is waiting for it to resolve, then using that resolve to resolve another promise with the same data. You don't need to bother; just return the $http chain. Admittedly, they could be a little clearer about then() in the documentation for $http and $q.

Upvotes: 0

Rafael Dantas
Rafael Dantas

Reputation: 78

.factory('movieService', function ($http, $log, $q)
{
    return {
        getMovie: function (movie)
        {
            var deferred = $q.defer();
          
            $http.get('/api/v1/movies/' + movie).then(
              
                //Success as first parameter
                function (data)
                { 
                    deferred.resolve({
                        title: data.title,
                        cost: data.price
                    });
                },
              
                // Error as second parameter
                function (msg, code)
                {
                    deferred.reject(msg);
                    $log.error(msg, code);
                }
            );
          
            return deferred.promise;
        }
    }
 });

Upvotes: 0

Kristiyan Kostadinov
Kristiyan Kostadinov

Reputation: 3712

Your code can be rewritten as:

.factory('movieService', function($http, $log, $q) {
    return {
        getMovie: function(movie) {
            var deferred = $q.defer();

            $http.get('/api/v1/movies/' + movie).then(function(response){
              var data = response.data;

              deferred.resolve({
                  title: data.title,
                  cost: data.price
              });
            }, function(msg, code) {
                deferred.reject(msg);
                $log.error(msg, code);
            });

            return deferred.promise;
        }
    };
});

Although you're doing a bit more work than necessary. It can be shortened down to:

.factory('movieService', function($http, $log, $q) {
    return {
        getMovie: function(movie) {
            return $http.get('/api/v1/movies/' + movie).then(function(response){
              var data = response.data;

              return {
                  title: data.title,
                  cost: data.price
              };
            }, function(msg, code) {
                $log.error(msg, code);
            });
        }
    };
});

Upvotes: 1

Related Questions