我有一段代码,如下所示
getInformations().subscribe(
informations => {
let subs = [];
for (const information of informations) {
subs.push(getOtherDetails(information.id));
}
forkJoin(subs).subscribe(response => {
//How can I Associate Information Id With The Response
howToAssociateIdWithResponse();
}}
);
Situation-我想将第二个电话的响应与第一个电话的ID绑定,但我遇到了问题.
Attempted-我try 了以下方法,但似乎出错了
let subs: {[x:number]: Observable<any>}[] = [];
subs.push({information.id: getOtherDetails(info.id)})
但是当我订阅的时候,我收到了一个错误,说是You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
Update - 1在遵循@BizzyBob建议之后,代码如下所示,但我的另一个逻辑在订阅完成其作业(job)之前运行.这就是我的意思
async ngOnChanges(){
await getData(); //How to make sure below method only executes when this is really done.
await useInformationReceivedFromgetData();
}
async getData(){
getInformations().subscribe(
informations => {
let subs = [];
for (const information of informations) {
subs.push(getOtherDetails(information.id).pipe(
map(data => ({ id: information.id, data })) // <---
));
}
forkJoin(subs).subscribe(objects => {
objects.forEach(({id, data}) => { /* saved to an arrray */ });
});
}
);
}