gpt4 book ai didi

javascript - 带下划线的 Angular 同步http调用?

转载 作者:行者123 更新时间:2023-12-03 09:36:38 25 4
gpt4 key购买 nike

这是我的代码:

$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);
})
};

此代码由于_.each而中断

我搜索了SO并发现了这个:

function processCoolStuff(coolStuffs) {
return $q.all(_.map(coolStuffs, makeStuffCooler));
}
processCoolStuff(…).then(showAllMyCoolStuff);

但是我无法让它工作,因为我有太多的异步函数。

我可以得到大约 10000 个帖子和 10000 个 http 调用,如果我可以使用上面的方法,它会消耗大量内存吗?

如何解决?

最佳答案

我从来没有遇到过这样的问题,但不要使用 _.each,而是使用 angular.forEach

angular.forEach(ids, function(id, index, list) {}

关于javascript - 带下划线的 Angular 同步http调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31314800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com