I am fairly new to AngularJS and Javascript. I am currently working on an application where I intend to use a single function for all Ajax operations (a factory in AngularJS). This function will serve as the gateway for all Ajax requests. While returning a promise as successful works fine, returning an error promise does not seem to work.
Below is a sample code snippet. I expect the error function to return a promise if the initial promise fails, but it always triggers the success function instead.
var myApp = angular.module('myApp', []);
myApp.controller('FirstController, function($scope, util){
util.doAjax().then(function(response){
// This function gets called in both success and error scenarios
}, function(err){
// This block is never executed, which raises questions
});
});
myApp.factory('util, function($http){
return $http({
method: 'GET',
url: 'http://www.example.com'
}).then(function(response){
// This returns a success promise
return response;
}, function(err){
// Expecting this block to return an error promise
return err;
});
});