1

various people use $http different way. say sample 1

$http({
        method: 'GET',
        url: 'api/book/',
        cache: $templateCache
    }).
    success(function (data, status, headers, config) {
        $scope.books = data;
    }).
    error(function (data, status) {
        console.log("Request Failed");
    });

here success and error callback is there to notify user. again few people use $http different way like

this.getMovie = function(movie) {
    return $http.get('/api/v1/movies/' + movie)
           .then(
              function (response) {
                return {
                   title: response.data.title,
                   cost:  response.data.price
                });
              },
              function (httpError) {
                 // translate the error
                 throw httpError.status + " : " + 
                       httpError.data;
              });
};

here then is using.......is it promise sample ? why people would then instead of success ? what is the advantage of then ?

what is the meaning of promise and what promise does ? when to use promise in angular ?

Monojit Sarkar
  • 2,353
  • 8
  • 43
  • 94
  • 3
    depending on the version of angular, .success is deprecated. – alphapilgrim Jun 14 '17 at 14:10
  • 1
    Possible duplicate of [Angular HttpPromise: difference between \`success\`/\`error\` methods and \`then\`'s arguments](https://stackoverflow.com/questions/16385278/angular-httppromise-difference-between-success-error-methods-and-thens-a) – anoop Jun 14 '17 at 14:12
  • @alphapilgrim from which version success got deprecated ? – Monojit Sarkar Jun 14 '17 at 14:20
  • Please refer this - https://stackoverflow.com/questions/35329384/why-are-angular-http-success-error-methods-deprecated-removed-from-v1-6 – Alexis Jun 14 '17 at 14:38

1 Answers1

0

Per angular DOCS, looks like 1.4.4 is the first version to have the notice(see below).

Deprecation Notice

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.

From the docs, the new preferred angular way to do all $http requests.(code from angular docs)

General usage
The $http service is a
function which takes a single argument— a configuration object— that is used to generate an HTTP request and returns a promise.

// Simple GET request example :
$http.get('/someUrl').
then(function(response) {
  // this callback will be called asynchronously
  // when the response is available
}, function(response) {
  // called asynchronously if an error occurs
  // or server returns response with an error status.
});
// Simple POST request example (passing data) :
$http.post('/someUrl', {
  msg: 'hello word!'
}).
then(function(response) {
  // this callback will be called asynchronously
  // when the response is available
}, function(response) {
  // called asynchronously if an error occurs
  // or server returns response with an error status.
});
Community
  • 1
  • 1
alphapilgrim
  • 3,761
  • 8
  • 29
  • 58