I have some angular factories for making ajax calls towards legacy ASP.NET .asmx web services like so:
module.factory(\'productService\', [\"$http\",
function ($
Interesting question!
As you mentioned yourself, $timeout
is the most logical choice for a delayed call. Instead of having $timeout
calls everywhere, you could push a response interceptor that wraps the $http
promise in a $timeout
promise, as conceptually outlined in the documentation of $http, and register it in one of your configuration blocks. This means all $http
calls are affected by the $timeout
delay. Something along the lines of:
$httpProvider.interceptors.push(function($timeout) {
return {
"response": function (response) {
return $timeout(function() {
return response;
}, 2500);
}
};
});
As a bonus to your "to simulate a bad connection?", you could reject or do absolutely nothing randomly, too. Heh heh heh.
While @stevuu's answer is correct, the syntax has changed in the newer AngularJS versions since then. The updated syntax is:
$httpProvider.interceptors.push(["$q", "$timeout", function ($q, $timeout) {
function slower(response) {
var deferred = $q.defer();
$timeout(function() {
deferred.resolve(response);
}, 2000);
return deferred.promise;
}
return {
'response': slower
};
}]);