My goal is to efficiently load around 10000 resources, but loading them all at once during the resolve phase is taking too long due to some calculations. I then had the idea to load the resources page by page sequentially, however, since all resources need to be visible on a map, standard user-input based pagination is not suitable.
I am aware that promises can be chained using:
promise.then(doThis).then(doThat).then(doWhat);
And I also know that an array of promises can be resolved with $q.all
like:
$q.all([doThis, doThat, doWhat]);
However, what I want to achieve is to repeatedly call the same promise in a series until it encounters a rejection.
For example:
function next() {
var deferred = $q.defer();
if(someCondition) {
deferred.reject();
} else {
//do something
//store data somewhere
deferred.resolve();
}
return deferred.promise;
}
Imagine that this function makes some $http
calls and saves the result in a service/controller. If it reaches a certain condition (like no more pages or an HTTP error), it will reject a promise; otherwise, it will resolve it.
Now, I would like to implement something like this pseudocode:
$q.loop(next).untilError(handleError);
Where the next
function will be called in a loop after resolving the previous next
call until it encounters a rejection.
Is it possible to achieve this kind of functionality?