Trying to get nested async REST requests in AngularJS (1.6), and then execute code when all requests have been completed.
I tried using $q.all but it seems this will not take into account the inner requests (the episode ones).
How do I modify the below example so that I can execute code when all requests have been completed?
// foreach series --> load series details & seasons, foreach season --> load episodes
// when all requests are completed: do something
var requests = []
// e.g. series = [1]
series.forEach(function(seriesId) {
requests.push(loadSeriesDetails(seriesId).then(function(data) {
console.log("received details for series "+seriesId)
// e.g. {id:1, title:"show 1"}
}))
requests.push(loadSeasons(seriesId).then(function (data) {
console.log("received seasons for series "+seriesId+": ", data.seasons.length)
// e.g. [{id:11, title:"season 1"}, {id:12, title:"season 2"}]
data.seasons.map(function(e) {return e.id}).forEach(function(seasonId) {
requests.push(loadEpisodes(seasonId).then(function (data) {
console.log("received episodes for season "+seasonId+": ", data.episodes.length)
// e.g.
// season 11: [{id:111, title:"episode 1-1-1"}, {id:112, title:"episode 1-1-2"}, {id:113, title:"episode 1-1-3"}]
// season 12: [{id:121, title:"episode 1-2-1"}, {id:122, title:"episode 1-2-2"}]
}))
})
}))
})
$q.all(requests).then(function(result) {
console.log("*** all requests completed ***")
console.log(result.length)
})
The example above would return 2 (1x loadSeriesDetails, 1x loadSeasons) instead of 4 (1x loadSeriesDetails, 1x loadSeasons, 2x loadEpisodes).
Suggestions? Thanks in advance!
Update: each of the is request functions is like:
loadSeriesDetails = function(id) {
url = "..."+id
return $http.get(url).then(
function (result) {
return result.data
}, function (error) {
̶r̶e̶t̶u̶r̶n̶ ̶e̶r̶r̶o̶r̶
throw error;
});
}