Angular 一次触发 2 个异步调用,但在第一个回调完成之前不处理第二个回调
Angular fire 2 async calls at once but do not process the second callback until the first finishes
我正在使用 Angular 的 $q 服务来发出异步请求。我有 2 个这样的请求(假设我有一个名为 MyService 的 angular 服务来处理这些请求):
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
});
我不能保证第二次调用会在第一次调用之后完成,但我需要调用 1 的结果才能在调用 2 中进行处理。我知道我可以将承诺链接在一起,这意味着调用 2在发出请求之前等待调用 1 完成,但我想同时触发两个请求,因为我拥有这样做所需的所有数据。最好的方法是什么?
编辑:我可以立即使用第一次调用的结果。他们在我的页面上驱动了一些图表。我不希望第一个电话等待第二个电话进行处理。我认为这排除了 $q.all()
这样的机制
您可以在 all
的同时进行这两个调用
$q.all([MyService.Call1(), MyService.Call2()]).then(function() {
// ...code dependent on both calls resolving.
});
编辑:在回复评论时,有两件事您可能会感兴趣。如果将一个数组传递给 all
,您会发现一个分辨率数组作为 then
中函数的第一个参数。相反,如果您将一个对象传递给 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]
});
...和对象
$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
});
使用 $q.all
的替代方法是在第二个处理程序中使用第一个承诺。例如
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
});
});
为了解决一些评论,这里是您可以处理错误/承诺拒绝的方式,而不必等待两个承诺都解决或创建多个 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
});
我正在使用 Angular 的 $q 服务来发出异步请求。我有 2 个这样的请求(假设我有一个名为 MyService 的 angular 服务来处理这些请求):
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
});
我不能保证第二次调用会在第一次调用之后完成,但我需要调用 1 的结果才能在调用 2 中进行处理。我知道我可以将承诺链接在一起,这意味着调用 2在发出请求之前等待调用 1 完成,但我想同时触发两个请求,因为我拥有这样做所需的所有数据。最好的方法是什么?
编辑:我可以立即使用第一次调用的结果。他们在我的页面上驱动了一些图表。我不希望第一个电话等待第二个电话进行处理。我认为这排除了 $q.all()
这样的机制您可以在 all
$q.all([MyService.Call1(), MyService.Call2()]).then(function() {
// ...code dependent on both calls resolving.
});
编辑:在回复评论时,有两件事您可能会感兴趣。如果将一个数组传递给 all
,您会发现一个分辨率数组作为 then
中函数的第一个参数。相反,如果您将一个对象传递给 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]
});
...和对象
$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
});
使用 $q.all
的替代方法是在第二个处理程序中使用第一个承诺。例如
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
});
});
为了解决一些评论,这里是您可以处理错误/承诺拒绝的方式,而不必等待两个承诺都解决或创建多个 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
});