2

I want to ask about the difference about this method My concern is difference between .then and .success, function and also .error thanks.

// Simple GET request example:
$http({
   method: 'GET',
   url: '/someUrl'
}).then(function successCallback(response) {
   // this callback will be called asynchronously
   // when the response is available
}, function errorCallback(response) {
   // called asynchronously if an error occurs
   // or server returns response with an error status.
});

and

// Simple GET request example:
$http({
   method: 'GET',
   url: '/someUrl'
}).success(function successCallback(response) {
   // this callback will be called asynchronously
   // when the response is available
}).error( function(data) {
   // called asynchronously if an error occurs
   // or server returns response with an error status.
});
Hardik Vaghani
  • 2,163
  • 24
  • 46
santro
  • 67
  • 2
  • 10
  • 1
    http://www.syntaxsuccess.com/viewarticle/angular-promises%E2%80%93then-vs-success – rejo Jul 28 '16 at 05:23
  • 2
    Don't use success and error, use then and catch. – dfsq Jul 28 '16 at 05:26
  • 3
    `.success` and `.error` are deprecated. – Claies Jul 28 '16 at 05:27
  • 1
    Find the documentation for promise here: http://stackoverflow.com/documentation/javascript/231/promises#t=201607280526409106492 – Varit J Patel Jul 28 '16 at 05:27
  • Possible duplicate of [Angularjs - Difference between success and then](http://stackoverflow.com/questions/16385278/angular-httppromise-difference-between-success-error-methods-and-thens-a) – Shubh Jul 28 '16 at 06:05
  • so can we add .error to catch error after using .then? – santro Jul 28 '16 at 08:44
  • @vanculix no, you don't use `.error` with `.then`. your own example in the question body shows the correct way to handle errors with `.then`. – Claies Jul 28 '16 at 16:25

1 Answers1

6

Both .then() and .sucess() are refer to promise it run asynchronously and wait for the response if it fullfied your request then resolve it otherwise reject it.

.success and .error are deprecated you can find more details on documentation

Wasiq Muhammad
  • 3,080
  • 3
  • 16
  • 29