[英]How can I wait for 9 HTTP GET request Angular 2
I have the next problem: I am doing a Plugin for Jenkins using its API REST. 我有下一个问题:我正在使用其API REST为Jenkins做一个插件。 I want to get all the results of all builds and do the average fails/nºexec for each Job. 我想获取所有构建的所有结果,并为每个作业执行平均失败/执行失败次数。 I want to get the last 10 builds, so I have to send 10 http get. 我想获取最近的10个版本,因此我必须发送10个http get。 Actually my code is this: 其实我的代码是这样的:
for (var j = init; j >= 1; j--){
this.http.get(data2.builds[j].url + this.finalURL).subscribe(response => {
let data3 = response.json();
if(data3.result == "FAILURE"){
numberOfFails = numberOfFails+1;
}
}, error => console.error(error));
}
this.http.get(data2.builds[0].url + this.finalURL).subscribe(response => {
let data3 = response.json();
if (data3.result == "FAILURE"){
numberOfFails = numberOfFails + 1;
}
let average = numberOfFails/(init+1);
console.log(numberOfFails +" " + init+1 + " " +average);
if (average <= 0.1){
this.successList.push(new Job(data3.fullDisplayName, data3.result,data2.url, average));
} else if (average<0.8){
this.brokenList.push(new Job(data3.fullDisplayName, data3.result, data2.url, average));
}else {
this.failureList.push(new Job (data3.fullDisplayName, data3.result, data2.url, average));
}
I do 9 HTTP request looking for the result. 我做9 HTTP请求寻找结果。 After I do the last request looking for the result and I do the average. 在我执行最后一个请求后,寻找结果,然后进行平均。 Sometimes the average is done before the 9 previous request finish. 有时,平均值是在前9个请求完成之前完成的。 How can I fix this? 我怎样才能解决这个问题?
You can use Observable.zip or Observable.forkJoin to resolve them all at once, and get the results at once.. this is similar to Promise.all. 您可以使用Observable.zip或Observable.forkJoin一次解决所有问题,并立即获得结果。这类似于Promise.all。 I would personally use forkJoin in this case http://xgrommx.github.io/rx-book/content/observable/observable_methods/forkjoin.html 在这种情况下,我将亲自使用forkJoin http://xgrommx.github.io/rx-book/content/observable/observable_methods/forkjoin.html
var source = Rx.Observable.forkJoin(
Rx.Observable.return(42),
Rx.Observable.range(0, 10),
Rx.Observable.from([1,2,3]),
RSVP.Promise.resolve(56)
//Your http calls will be here instead
);
var subscription = source.subscribe(
function (x) {
console.log('Next: %s', x);
},
function (err) {
console.log('Error: %s', err);
},
function () {
console.log('Completed');
});
// => Next: [42, 9, 3, 56]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.