JSFiddle - React, Tailwind, and code Playground
HTML
<body ng-app="myApp" ng-controller="myCtrl">
</body>
JavaScript
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http, $q) {
$scope.request = function request(object) {
var counter = 0;
var deferred = $q.defer();
var httpConfig = {
method: 'GET',
url: '/echo/json'
}
var doRequest = function() {
counter++;
var self = this,
args = arguments;
$http(httpConfig).
success(function(data, status) {
if (data.key == 'your_droid') {
deferred.resolve('Woohoo!');
} else {
console.log('Not the droids you are looking for..');
//this will re-call doRequest();
args.callee.apply(self);
}
}).
error(function(data, status) {
//just fail already, it's not working
if (counter > 5) {
return deferred.reject('Couldnt reach server this time...');
}
//this will re-call doRequest();
args.callee.apply(self);
});
}
doRequest();
return deferred.promise;
}
$scope.request();
});