This is my code:
$scope.updatePosts = function() {
Posts.getIdsFromServer('http://localhost/postIds')
.then(function(ids) {
_.each(ids, function(id) {
Posts.getByIdFromDb(id)
.then(function(p) {
if(p) {
if(p.enabled) {
Posts.getFromServer('http://localhost/post/' + id)
.then(function(post) {
Posts.update(post);
})
}
} else {
Posts.getFromServer('http://localhost/post/' + id)
.then(function(post) {
Posts.insert(post);
})
}
});
});
}, function(error) {
console.log(error);
})
};
This code breaks because of _.each
I searched SO and found this:
function processCoolStuff(coolStuffs) {
return $q.all(_.map(coolStuffs, makeStuffCooler));
}
processCoolStuff(…).then(showAllMyCoolStuff);
But I cannot make it work, becasue I have too many async functions.
And I can get like 10000 posts and 10000 http calls, if I can use above method, will it consume a lot of memory?
How to solve it?
I never had such a problem, but instead of _.each
, use angular.forEach
angular.forEach(ids, function(id, index, list) {}