RxJS在循环中创建Observables



我想在循环中创建Observable,并等待它们全部完成。

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

patchSlave((函数返回一个Observable。

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

我不知道解决这个问题的最佳方法。我想我必须省略循环a用Rxjs中的东西替换a?

此处使用RxJSforkJoin运算符。

您可以传递可观察性数组,当所有可观察器都完成时,它将给出最终值。

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)});

请参阅此处了解更多详细信息:RxJS forkJoin

最新更新