javascript - Repeat HTTP requests until 404 error is returned -
i send http requests until 1 of them responds error 404
.
there 21 pages , have this:
_getall = function () { var promises = []; var pages = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]; angular.foreach(pages, function (page) { var deffered = $q.defer(); $http({ url: 'http://api.tvmaze.com/shows?page=' + page, method: 'get' }). success(function (data) { console.log("ok") deffered.resolve(data); }). error(function (error) { deffered.reject(error); console.log(error.status); }); promises.push(deffered.promise) }) return $q.all(promises); },
but if try http://api.tvmaze.com/shows?page=22 returns 404 error
.
so there option make http
request until 1 of them returns 404
. while looping or somehow else?
these calls asynchronous while loop try make calls 1000s of pages in couple ms before you'd load first page. need wait each page complete before loading next page.
one way make function page , trigger next page number each time load page. once hit error trigger final success callback , pass data back.
_getall = function(callback) { var pagedata=[]; function getpage(page) { $http({ url: 'http://api.tvmaze.com/shows?page=' + page, method: 'get' }). success(function (data) { pagedata.push(data); //get next page getpage(page + 1); console.log("ok") }). error(function (error) { //hit error. done. trigger callback. callback(pagedata); console.log(error.status); }); } //get first page getpage(0); } //usage: _getall(function(data){ //this function trigger once error //data array of of pages data console.log(data); });
Comments
Post a Comment