[英]chaining multiple http calls against a loop
I've found all kinds of useful posts/info about $q, $q.all, and chaining in general, but I haven't found any examples of my exact issue, so I'm wondering whether what I want to do is even possible. 我已经找到了关于$ q,$ q.all和链条的各种有用的帖子/信息,但是我没有找到确切问题的任何示例,所以我想知道我想做的是甚至可能。 I've got a series of calls I need to make, and each is dependent on the previous BUT there's a step in the middle where the series splits into several parallels. 我需要进行一系列的调用,每个调用都取决于先前的BUT,但是中间有一个步骤,其中该系列分为几个平行部分。 The gist of it goes something like this: 它的要旨是这样的:
call #1: get keyword (based on inputs)
call #2: with keyword, get list of items
call #3: with item ID#s, get item profiles
call #4: with data in profiles, get item images
(I know, I know, four calls for just one is ridiculous, let alone sixteen calls total, but this is for a prototype. I don't have to be ultra-speedy. I just need to prove I can get to that final step with the existing data/calls.) (我知道,我知道,只有四个电话很荒谬,更不用说总共十六个电话了,但这只是为了一个原型。我不必太快。我只需要证明自己可以打入决赛处理现有的数据/调用。)
Anyway, this means that #1 is one call for all... and then as of #2, it splits up and I have to do #2, #3, and #4 for each different ID# I got from #1's return. 无论如何,这意味着#1是所有人的一个电话...然后从#2开始,它分裂了,我必须为从#1的返回中获得的每个不同的ID做#2,#3和#4。 。 I've mocked up a plunk using jsonplaceholder. 我使用jsonplaceholder模拟了一个小问题。 It should retrieve the first set, then do the next two calls with the retrieved IDs, one loop per ID. 它应该检索第一个集合,然后使用检索到的ID进行下两个调用,每个ID循环一次。
I've tried a simple for loop (which Plunker tells me I can't do w/a function in it). 我尝试了一个简单的for循环(Plunker告诉我我不能在其中执行功能)。 I've tried an angular.forEach (inside a $scope.watch and outside it), but that doesn't seem to get me anything at all: 我已经尝试过angular.forEach(在$ scope.watch内部和外部),但这似乎一点也没给我带来什么:
angular.forEach($scope.resultsb, function(value, key) {
console.log(key + ': ' + value);
$http.get('http://jsonplaceholder.typicode.com/users?id='+ value)
.then(function(res2){
$scope.data2 = res2.data;
var obj = {
title: $scope.results1[i].title,
id: $scope.data2[i].id,
username: $scope.data2.username
};
$scope.results2.push(obj);
I think the forEach just isn't kicking in properly b/c it can't really run until after I get $scope.results1 (the first step's results), but can I chain an $http call to a loop? 我认为forEach只是无法正确启动b / c,直到获得$ scope.results1(第一步的结果)之后,它才能真正运行,但是我可以将$ http调用链接到循环吗?
You can see the full thing (in its current version, which may change since I continue to beat at it) at http://plnkr.co/edit/CjXt7E?p=preview . 您可以在http://plnkr.co/edit/CjXt7E?p=preview上看到完整的内容(在当前版本中,可能会有所更改,因为我会继续对此予以反对)。 Is what I'm trying even possible, or do I have to do this long-hand? 我正在尝试的甚至有可能吗?还是我必须长期这样做?
Your for each will run even without response as it is Asynchronous, instead make a function and call it when you get the callback in .then() 因为它们是异步的,所以for的即使没有响应也可以运行,而是创建一个函数并在.then()中获得回调时调用它
Edited your plunker: http://plnkr.co/edit/mxvvCB?p=preview 编辑了您的监听器: http ://plnkr.co/edit/mxvvCB?p=preview
like this: 像这样:
$scope.Call = function(value) {
if(typeof value[$scope.counter] !== 'undefined'){
value = value[$scope.counter].id;
var i =$scope.counter;
console.log(value+' ' +i);
$http.get('http://jsonplaceholder.typicode.com/users?id='+ value)
.then(function(res2){console.log(res2);
$scope.data2 = res2.data;
var obj = {
title: $scope.results1[i].title,
id: $scope.data2[0].id,
username: $scope.data2.username
};
$scope.results2.push(obj);
$scope.counter++;
$scope.Call($scope.resultsb);
});
}
};
I hope it is what you are trying to achieve. 我希望这是您要实现的目标。
I GOT IT TO WORK. 我得到了工作。
This might be the worst case of function-use ever, but it works, and I get results. 这可能是有史以来最糟糕的函数使用情况,但是它可以正常工作,并且我得到了结果。 Not entirely -- I think some of the calls time out, so I figured out I need to ask for, like, 6, and then I get at least the 4 results I want. 不完全是-我认为有些通话超时,所以我发现我需要问一些类似6的问题,然后我至少得到了我想要的4个结果。 Which is probably indicative of the fact that this desperately calls for a single endpoint instead of doing the logic on the frontend, but it works enough for a prototype, so I'm happy (for now). 这很可能说明了这一事实,即它拼命需要一个端点,而不是在前端执行逻辑,但是对于原型来说它已经足够用了,所以(现在)我很高兴。
In my controller, I get the first set of IDs, and using a simple for-loop, I assign a loop-id to each. 在我的控制器中,我获得了第一组ID,然后使用简单的for循环为每个ID分配一个循环ID。 Then I call getUser(), with num (to connect the info to the first set of results), and id (to connect to the actual user I'm tracking). 然后,我调用getUser(),使用num(将信息连接到第一组结果)和id(连接到我正在跟踪的实际用户)。 Here's the function w/ its internal chains: 这是带有内部链的函数:
function getUser(num, id) {
$http.get('http://jsonplaceholder.typicode.com/users?id='+ id)
.then(function(res2){
$scope.data2 = res2.data;
if ($scope.data2[0].username !== 'undefined') {
var obj = {
num: $scope.results1[num].num,
id: $scope.results1[num].id,
title: $scope.results1[num].title,
username: $scope.data2[0].username
};
$scope.results2.push(obj);
}
return $http.get('http://jsonplaceholder.typicode.com/comments?id=' + id);
}).then(function(res3){
if ($scope.results2[num].username !== 'undefined') {
$scope.data3 = res3.data;
var obj = {
num: num,
id: $scope.results2[num].id,
title: $scope.results2[num].title,
username: $scope.results2[num].username,
email: $scope.data3[0].email
};
$scope.results3.push(obj);
}
});
}
You can see the entire thing at http://plnkr.co/edit/CjXt7E?p=preview -- it shows the output of each step (results1, results2, results3). 您可以在http://plnkr.co/edit/CjXt7E?p=preview上看到整个内容-它显示了每个步骤的输出(结果1,结果2,结果3)。 Hopefully I won't have to do something this crazy again, but just in case, maybe this will help someone. 希望我不必再做那么疯狂的事情,但是以防万一,也许这会帮助某个人。 Or maybe it'll cause someone to leap up and tell me everything I did wrong, but that'd be fine, too. 也许这会使某人跳起来,并告诉我我做错的所有事情,但这也可以。 Gotta learn somehow. 要以某种方式学习。 :) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.