I'm currently facing an issue with my code snippet below:
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 - My goal is to link the responses from the second call with the ids from the first call, but I'm encountering difficulties.
Attempted - I attempted the following approach which resulted in an error being thrown:
let subs: {[x:number]: Observable<any>}[] = [];
subs.push({information.id: getOtherDetails(info.id)})
However, upon subscription, I received an error message stating
You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
Update - 1 Following @BizzyBob's suggestion, I made adjustments to the code as shown below. However, I am finding that my other logic executes before the subscription completes its task. Here's what I mean:
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 */ });
});
}
);
}