[英]Angular fire 2 async calls at once but do not process the second callback until the first finishes
I'm using Angular's $q service to make async requests. 我正在使用Angular的$ q服务来发出异步请求。 I have 2 requests like so (assume I have an angular service named MyService that handles these requests):
我有2个这样的请求(假设我有一个名为MyService的角度服务来处理这些请求):
MyService.Call1().then(function() {
//do all the first callback's processing here without waiting for call 2
});
MyService.Call2().then(function() {
//wait for results of first callback before executing this
});
I have no guarantee that the second call will finish after the first, but I need the results of call 1 in order to do processing in call 2. I understand that I could chain the promises together, meaning that call 2 waits for call 1 to finish before the request is made, but I want to fire both requests at the same time since I have all the data required to do so. 我不能保证第二次调用会在第一次调用之后完成,但我需要调用1的结果才能在调用2中进行处理。我知道我可以将promises链接在一起,这意味着调用2等待调用1到在请求完成之前完成,但我想同时触发两个请求,因为我有所需的所有数据。 What's the best way to do that?
最好的方法是什么?
Edit: I can use the results of the first call immediately. 编辑:我可以立即使用第一个调用的结果。 They drive some charts on my page.
他们在我的页面上开了一些图表。 I don't want the first call to wait on the second call to do its processing.
我不希望第一次调用等待第二次调用来进行处理。 I think that rules out mechanisms like $q.all()
我认为这排除了$ q.all()等机制
You can do both calls in parallel with all
您可以与
all
并行执行两个呼叫
$q.all([MyService.Call1(), MyService.Call2()]).then(function() {
// ...code dependent on both calls resolving.
});
Edit : In response to a comment, there are two things that might interest you. 编辑 :在回复评论时,您可能会对两件事感兴趣。 If you pass an array to
all
, you will find an array of resolutions as the first argument of your function inside of then
. 如果你传递一个数组
all
,你会发现分辨率为你的函数内的第一个参数的数组then
。 If, instead, you pass an object to all
, you will find an object as your first argument with keys matching the same keys you passed into all
. 相反,如果你将一个对象传递给
all
,你会发现一个对象作为你的第一个参数,其中的键与你传递给all
键的键相同。
$q.all([MyService.Call1(), MyService.Call2()]).then(function(arr) {
// ...code dependent on the completion of both calls. The result
// of Call1 will be in arr[0], and the result of Call2 will be in
// arr[1]
});
...and with objects ......和对象
$q.all({a: MyService.Call1(), b: MyService.Call2()}).then(function(obj) {
// ...code dependent on the completion of both calls. The result
// of Call1 will be in abj.a, and the result of Call2 will be in
// obj.b
});
An alternative to using $q.all
would be to use the first promise in the handler for the second. 使用
$q.all
的另一种方法是在第二个处理程序中使用第一个promise。 For example 例如
var p1 = MyService.Call1().then(function(data) {
return processedData;
});
MyService.Call2().then(function(call2Data) {
return p1.then(function(call1Data) {
// now you have both sets of data
});
});
To address some comments, here's how you could handle errors / promise rejections without having to wait for both promises to resolve or creating multiple catch
handlers... 为了解决一些意见,这里是你能怎么处理错误/而不必等待所有承诺解决或创建多个承诺拒绝
catch
处理程序...
var p2 = MyService.Call2().then(function(call2Data) {
return p1.then(function(call1Data) {
// now you have both sets of data
});
});
// use `$q.all` only to handle errors
$q.all([p1, p2]).catch(function(rejection) {
// handle the error here
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.