JSFiddle - React, Tailwind, and code Playground
by chandings
HTML
<script src="http://code.angularjs.org/angular-1.0.0.js"></script>
<div ng-app="myApp" ng-controller="myController">
<button ng-click="clickHandler1()">Success Call</button>
<button ng-click="clickHandler2()">Error Call</button>
</div>
JavaScript
var myApp = angular.module('myApp', []);
myApp.controller("myController", ["$scope", "myService", function (scope, myService) {
scope.clickHandler1 = function () {
var promise = myService.getRealData();
promise.then(function (data) {
if(data.statusCode === "error"){
alert("error: " + data.msg)
}
else{
alert("Success")
}
});
};
scope.clickHandler2 = function () {
var promise = myService.getNoData();
promise.then(function (data) {
if(data.statusCode === "error"){
alert("error: " + data.msg)
}
else{
alert("Success")
}
});
}
}]);
myApp.factory("myService", ['httpWithRetries', function (http) {
return {
getRealData: function () {
return http.send({
method: 'POST',
data: 'Its Working',
url: '/echo/html/'
});
},
getNoData: function () {
return http.send({
method: 'POST',
data: 'Its Working',
url: 'http://example.com/dummy'
});
}
}
}]);
myApp.provider("httpWithRetries", function () {
var retries = 3;
var interval = 1000;
this.setRetryCount = function(value){
retries = value;
}
this.setInterval = function(value){
interval = value;
}
this.$get = ["$http", "$q", "$timeout", function (http, q, timeout) {
var returnObject = {};
returnObject.tryCount = 0;
returnObject.send = function (data, chainedDeffere) {
console.log(this.tryCount);
var currObject = this;
var deferred = (chainedDeffere)?chainedDeffere:q.defer();
console.log("httpWithRetry url: " + data.url);
http(data)
.success(function (result) {
...