How do I add a callback function after an async forEach Loop?
Here is some better context:
$scope.getAlbums = function(user, callback) {
$scope.getAlbums = function(user, callback) {
var promiseArr = [];
$scope.albumsList.forEach(function (obj, i) {
var anHttpPromise =
$scope.getAlbum(user, obj.id, function(value){
$scope.albums.push(value);
});
promiseArr.push(anHttpPromise);
});
$q.all(promiseArr).then(function(){
// This callback function will be called when all the promises are resolved. (when all the albums are retrived)
})
};
$scope.getAlbum = function(user, id, callback) {
var anHttpPromise = Imgur.album.get({user:user, id:id},
function(value) {
return callback(value.data);
}
);
return anHttpPromise;
}
In the above code:
getAlbum is made to return an promise.getAlbums list$q.all$q.all method instead returns an final promise whose callback
function will be triggered once all the promises in the array are
resolved.