Suppose I have a list of URLs as follows:
var myurls = ['http://server1.com', 'http://server2.com', 'http:sever2.com', etc ]
Each URL is considered a "fallback" and should only be used if the previous one is unreachable. This list establishes a priority order. Let's also assume that this list can vary in length - its size is unknown and needs to be iterated through.
How can I write a function, named "reachability", that loops through this array and returns the first server that can be reached?
I cannot use $http.all
since it executes requests in parallel. Additionally, running a while
loop with an $http.get
within it is not feasible because the response may be delayed, causing the user interface to freeze in the meantime.
It's worth mentioning that jQuery is not being utilized; instead, Ionic framework, which includes a version of jQuery-lite, is being used.
While many examples propose chaining promises using .then
, this approach assumes prior knowledge of the number of URLs, which is not the case here.
Thank you.