简体   繁体   中英

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.

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

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM