Imagine you have an article with comments.
The data structure looks something like this:
{ArticleId...., someFields...., Comments: [{AuthorId:1, Text:''}, {AuthorId:2, Text:''}, {AuthorId:3, Text:''}]}
When trying to retrieve user information (such as avatar and name) through the endpoint: /user/{id}
(this is only triggered when a user clicks on the comments...)
$scope.getUserData = function(el) {
$http.get(settings.apiBaseUri + '/app/users/' + el.AuthorId, {
headers: {
'Content-Type': 'application/json',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'If-Modified-Since': ''
}
})
.success(function(response) {
/*store some data*/
});
});
$scope.getArticleData = function(){
angular.forEach($scope.article.Comments, function(el) {
$scope.getUserData(el.AuthorId);
});
/*How can I ensure that my forEach loop has completed all tasks (including loading of all http data) before running a new method?*/
};
How can I ensure that my forEach loop has completed all tasks (including loading of all http data) before running a new method?