Skip to content

Instantly share code, notes, and snippets.

@zgohr
Created February 19, 2013 18:31
Show Gist options
  • Save zgohr/4988556 to your computer and use it in GitHub Desktop.
Save zgohr/4988556 to your computer and use it in GitHub Desktop.
Javascript recursion (recursive function) with promises
var getAll = function(url, page, acquired, deferred) {
var self = this;
page = page || 1;
deferred = deferred || $q.defer();
var page_url = [url, '&page=', page].join("");
$http.get(page_url).then(function(results) {
acquired = acquired ? acquired.concat(results.data) : results.data;
if (results.data.length === 100) { // Assuming the max is 100 per page...
return self.getAll(url, ++page, acquired, deferred);
} else {
deferred.resolve(acquired);
}
});
return deferred.promise;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment