I am facing a challenge with my project. My framework of choice is AngularJS and I do not have access to jQuery or Lodash.
The Issue
In my code, I have a function named "refresh". This function initiates an asynchronous call using angular $http to retrieve new data from the server. The server provides 25 new updates starting from the specified date. To fetch all the new messages, I need to continuously make requests to the server (updating the "updateDate" each time I receive new data) until there are no more messages (empty array).
Example Code
$scope.refresh = function () {
var date = new Date();
$http({
method: 'GET',
url: 'http://path.to.my.server',
timeout: 6000
}).then(function (success) {
date = success.date[0].date;
callback(success.data);
//Data manipulation
}, function (error) {
console.error("Could not retrieve new messages: \n", error.data);
errcallback(error);
});
}
Attempts Made
I attempted to separate the request into a different function and call it multiple times as you would with a standard async function.
I also tried utilizing a while loop and using a boolean variable to indicate when data collection is complete. However, the issue with a while loop is that it does not wait for the call to finish, resulting in a potentially infinite loop that could crash the program.
Considering using a for loop, but unsure about the number of iterations required. It might be just one, but it could also be five or even more.
I understand how recursive functions operate, but implementing an async recursive function is where I face difficulty. Any suggestions or solutions are highly appreciated. (If there's an alternative approach that doesn't involve recursion, I'm open to exploring that as well)