Home  >  Forum  >  JavaScript
Post New Query

Difference between AngularJS .then and .success callback


joined on
April 13,2016
Asked on April 14,2016
What is difference between AngularJS .then and .success callback?

joined on
October 10,2013
Replied on April 14,2016

.then in AngularJS

1. .then is called on HTTP promise object and returns new promise object.
2. Because .then returns new promise object, it can be used for sequential calling. 
3. As an argument it accepts only one argument for response. The properties of response is accessed as response.data, response.status etc.
4. .then is used along with .catch and .finally
5. Find the sample example.

var myApp = angular.module('myApp', []);
myApp.controller('myController', function($scope, $http){
    $http.get('sample_json.js')
.then(function (response){
$scope.jsondata = response.data;
console.log("status:" + response.status);
}).catch(function(response) {
 console.error('Error occurred:', response.status, response.data);
}).finally(function() {
console.log("Task Finished.");
});
});

.success in AngularJS

1. .success is called on HTTP promise and it returns the same promise object.
2. As an argument it accepts response properties such as data, status etc.
3. .success is used along with .error and .finally.
4. .success and .error has been deprecated.
5. Find the sample example.

var myApp = angular.module('myApp', []);
myApp.controller('myController', function($scope, $http){
    $http.get('sample_json.js')
.success(function (data, status){
$scope.jsondata = data;
console.log("status:" + status);
}).error(function(data, status) {
   console.error('Error occurred:', data, status);
}).finally(function() {
console.log("Task Finished.");
});
}); 

Write Answer









Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us