1

I want to create Observables in a loop and wait for them all to be finished.

for (let slaveslot of this.fromBusDeletedSlaveslots) {
                    this.patchSlave({
                        Id: slaveslot.Id,
                        BusOrder: null,
                        BusId: null
                    });
                }

The patchSlave() Function returns a Observable.

patchSlave(slaveslot: any): Observable<any> {
    return this.httpClient.patch(environment.apiBaseUrl + `/odata/SlaveSlots(${slaveslot.Id})`, slaveslot);
}

I don't know the best way to solve this. I think I have to omit the loop a replace a with something from Rxjs?

1 Answer 1

2

Use here RxJS forkJoin operator.

You can pass array of observables, and it will give final value when all observables are completed.

let array = [];
for (let slaveslot of this.fromBusDeletedSlaveslots) {
               array.push ( this.patchSlave({
                        Id: slaveslot.Id,
                        BusOrder: null,
                        BusId: null
                    }));
                }

forkJoin(array).subscribe(results => {console.log(results)});

Refer here for more details : RxJS forkJoin

Sign up to request clarification or add additional context in comments.

2 Comments

Okay thank you very much!! Yeah I though of it. Is there a way without an array?
Consider using the merge? It's the next item in the provided link.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.