Created
November 6, 2015 14:32
-
-
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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