Skip to content

Instantly share code, notes, and snippets.

@colthreepv
Last active December 25, 2015 23:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colthreepv/7056188 to your computer and use it in GitHub Desktop.
Save colthreepv/7056188 to your computer and use it in GitHub Desktop.
AngularJS mocked backend demonstration, step-2
angular.module('appMock', ['ngMockE2E'])
.factory('delayHTTP', function ($q, $timeout) {
return {
request: function (request) {
var delayedResponse = $q.defer();
$timeout(function () {
delayedResponse.resolve(request);
}, 500);
return delayedResponse.promise;
},
response: function (response) {
var deferResponse = $q.defer();
if (response.config.timeout && response.config.timeout.then) {
response.config.timeout.then(function () {
deferResponse.reject();
});
} else {
deferResponse.resolve(response);
}
return $timeout(function () {
deferResponse.resolve(response);
return deferResponse.promise;
});
}
};
})
// delay HTTP
.config(['$httpProvider', function ($httpProvider) {
$httpProvider.interceptors.push('delayHTTP');
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment