Reputation: 3386
I have following setup:
let result = service.getResults();
result.data.forEach((row:any) => {
this.myService
.getObject(row.id)
.subscribe((object:any) => {
//...
})
);
}
I want do do something, after all the subscriptions of getObject(row.id)
have finished. I know there's the add()
function but it will do this for every Subscription. Coming from Promises I would have stored every Promise and then in an array and just called Promise.all().
Upvotes: 0
Views: 3071
Reputation: 3439
I want do do something, after all the subscriptions of getObject(row.id) have finished.
Better to use forkJoin for your example. From official docs:
forkJoin will wait for all passed Observables to complete and then it will emit an array with last values from corresponding Observables.
Try to do it in this way:
import { forkJoin } from 'rxjs';
const rowObjects$ = result.data.map(rd => this.myService.getObject(rd.id));
forkJoin(rowObjects$)
.subscribe((object:any) => {
//...
})
Upvotes: 4
Reputation: 8558
You can use combineLatest
to combine all the observables and wait until every observable fires at least once.
import {combineLatest} from 'rxjs';
...
let result = service.getResults();
const observablesToCombine = [];
result.data.forEach((row:any) => {
observablesToCombine.push(this.myService.getObject(row.id));
})
combineLatest(observablesToCombine).subscribe((object: Array<any>) => {
//...
})
Upvotes: -1