[英]Parallel http requests via forkJoin and response identification
I would like to make parallel independent calls to an endpoint.我想对端点进行并行独立调用。
First I construct my calls then I call forkJoin
.首先我构建我的调用然后我调用
forkJoin
。
getAllDirections(data: Object, date: string) {
let urls = [];
for (let elm in data) {
let url = `http:XXXXX?date=${date}&directions=${data[elm].All.join()}`;
urls.push(this.http.get<ISchedules>(url));
}
return forkJoin(urls).pipe(
map(dirData => {
let dataSorted = dirData.map(elm => _.groupBy(elm.data, 'direction'));
return dataSorted;
})
);
}
the data params is an objects of params that I pass to the URl数据参数是我传递给 URl 的参数对象
data = {
b1: [params1],
b2: [params2],
b3: [params3]
}
What I would as result be able to construct this object结果我能够构造这个对象
dataRes = {
b1: [resDataofParams1],
b2: [resDataofParams2],
b3: [resDataofParams3]
}
When I get the array response I should affect every array item to it's corresponding b{n}
, How can I get the responses in the same order that I passed in forkJoin
?当我得到数组响应时,我应该将每个数组项影响到它对应的
b{n}
,我怎样才能按照我在forkJoin
传递的相同顺序获得响应? Or it there a way to pass a parameter in this.http.get<ISchedules>(url)
and get it when I get data response ?或者有没有办法在
this.http.get<ISchedules>(url)
传递参数并在我收到数据响应时获取它?
In my view, it is better to use already created method forkJoin() from library RXJS
.在我看来,最好使用库
RXJS
已经创建的方法forkJoin() 。 In RXJS 6.5 you need to pass an array of observables :在 RXJS 6.5 中,您需要传递一组 observables :
const combined = Observable.forkJoin(
[(this.http.get('https://foourl1').map((res: Response) => res.json()),
of(this.http.get('https://foourl1').map((res: Response) => res.json())]
)
combined.subscribe(latestValues => {
const [ data_changes , data_all ] = latestValues;
console.log( "data_changes" , data_changes);
console.log( "data_all" , data_all);
});
forkJoin
will return data when all calls are finished and return result. forkJoin
将在所有调用完成后返回数据并返回结果。
Another example:另一个例子:
const request1 = this.http.get('https://restcountries.eu/rest/v1/name/india');
const request2 = this.http.get('https://restcountries.eu/rest/v1/name/us');
const request3 = this.http.get('https://restcountries.eu/rest/v1/name/ame');
const request4 = this.http.get('https://restcountries.eu/rest/v1/name/ja');
const requestArray = [];
requestArray.push(request1);
requestArray.push(request2);
requestArray.push(request3);
requestArray.push(request4);
forkJoin(requestArray).subscribe(results => {
console.log(results);
this.response = results;
});
All results are ordered accordingly pushed items into requestArray.
所有结果都按顺序排列,将项目推送到
requestArray.
It can be seen in a stackblitz example .它可以在stackblitz 示例中看到。
As of RxJS 6.5 you can pass a dictionary of Observables to forkJoin
to get an Object with the last response from every Observable.从 RxJS 6.5 开始,您可以将 Observable 字典传递给
forkJoin
以获取包含每个 Observable 的最后响应的对象。
const http = (url: string) => of("response for " + url);
const data = {
b1: ["b1-1", "b1-2"],
b2: ["b2-1", "b2-2"],
b3: ["b3-1", "b3-2"]
};
const date = '15.01.2020';
// map data to dictionary of http requests
const httpRequests = Object.keys(data).reduce((p, c) => {
const url = `http:XXXXX?date=${date}&directions=${data[c].join(',')}`;
return { ...p, [c]: http(url) };
}, {});
forkJoin(httpRequests).subscribe(console.log);
// output
{
b1: "response for http:XXXXX?date=15.01.2020&directions=b1-1,b1-2",
b2: "response for http:XXXXX?date=15.01.2020&directions=b2-1,b2-2",
b3: "response for http:XXXXX?date=15.01.2020&directions=b3-1,b3-2"
}
https://stackblitz.com/edit/rxjs-qecpud https://stackblitz.com/edit/rxjs-qecpud
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.