Skip to content

Instantly share code, notes, and snippets.

@cahlan
Created November 6, 2015 14:32
Show Gist options
  • Save cahlan/71ff198bc7d984a36661 to your computer and use it in GitHub Desktop.
Save cahlan/71ff198bc7d984a36661 to your computer and use it in GitHub Desktop.
example of using a parallelized promise to grab a collection of network data
this.getFilms = function(char) {
var deferred = $q.defer();
//this array will hold references to all of the $http calls that need to be made
var film_requests = [];
for (var i = 0; i < char.films.length; i++) {
film_requests.push(
$http({
method: 'GET',
url: char.film[i]
});
);
}
//now we use $q.all which returns a single promise for an array of promises that need to be executed
$q.all(film_requests).then(function(requests) {
for (var i = 0; i < requests.length; i++) {
char.films[i] = requests[i].data;
}
deferred.resolve(char);
});
return deferred.promise;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment