I am trying to create some logic in my api to exclude objects that have a URL in the "image" field, which returns 4xx, 5xx or times out for more than 4000 ms.
The code below does not work, as it still permits 404 and blank images.
Can someone tell me how to fix this?
In addition, here he is in plunker- http://plnkr.co/edit/7ScnGyy2eAmGwcJ7XZ2Z?p=preview
Example json: {id: 242, image: "www.image.com/1.jpg", name: "blah blah"}
.controller('CardsCtrl', ['$scope', '$http', '$state', '$q', '$ionicLoading', '$ionicModal', 'TDCardDelegate', 'cardsApi', '$http', '$timeout', '$element', function($scope, $http, $state, $q, $ionicLoading, $ionicModal, TDCardDelegate, cardsApi, $http, $timeout, $element) { var loginuid = window.localStorage.getItem('uid'); console.log(loginuid); var MIN_CARDS = 7; console.log('CARDS CTRL'); $scope.cards = []; cardsApi.getApiData() .then(function(result) { console.log(result.data); //Shows log of API incoming $scope.cards = result.data; result.data.forEach(function(card) { var imgurl = card.image; console.log(imgurl); var canceler = $q.defer(); $http.get(imgurl, { timeout: canceler.promise }) .then(function mySuccess(response) { console.log(response.status); }, function myError(response) { console.log(response.status); if (response.status == 404) { $scope.cards.splice(card, 1); } // console.log('deleted'); var imgpid = "#card-image-" + card.vari; console.log(imgpid); $(imgpid).parent().remove(); }) .catch(function(err) { }); $timeout(function() { $ionicLoading.hide(); canceler.resolve(); }, 4000); }); })