在所有可观察对象完成后订阅最终可观察对象?
Subscribe to final observable after all observable completed?
我这里有这个逻辑,它创建了许多可观察对象并在每个对象完成后执行回调。
from(rows).pipe(
concatMap(row => this.uploadItem(row))
).subscribe(response => {
this.currentRowNode.data.uploadStatus = UploadStatus.Success;
this.currentRowNode.data.cmsRowId = response.responsePayload.cmsRowId
this.currentRowNode.setData(this.currentRowNode.data);
});
currentRowNode: RowNode;
uploadItem(rowNode: RowNode): Observable<any> {
this.currentRowNode = rowNode;
rowNode.data.uploadStatus = UploadStatus.Uploading;
rowNode.updateData(rowNode.data);
return this.importService.uploadItem(rowNode.data)
}
我的问题是,我希望能够订阅一些内容,告诉我 uploadItem
已为每个 rows
调用
那会去哪里?
您可以像这样创建一个可观察对象
const final$ = of("completed");
并使用 rxjs concat 运算符,像这样将其与现有的可观察对象连接起来。
const myobs$ = from(rows).pipe(concatMap(row => this.uploadItem(row)));
concat(myobs$, final$).subscribe(response=>{
//your existing code here
if(response=="completed")
{
console.log('all observables completed');
}
});
这里我创建了一个example stackblitz。
我这里有这个逻辑,它创建了许多可观察对象并在每个对象完成后执行回调。
from(rows).pipe(
concatMap(row => this.uploadItem(row))
).subscribe(response => {
this.currentRowNode.data.uploadStatus = UploadStatus.Success;
this.currentRowNode.data.cmsRowId = response.responsePayload.cmsRowId
this.currentRowNode.setData(this.currentRowNode.data);
});
currentRowNode: RowNode;
uploadItem(rowNode: RowNode): Observable<any> {
this.currentRowNode = rowNode;
rowNode.data.uploadStatus = UploadStatus.Uploading;
rowNode.updateData(rowNode.data);
return this.importService.uploadItem(rowNode.data)
}
我的问题是,我希望能够订阅一些内容,告诉我 uploadItem
已为每个 rows
那会去哪里?
您可以像这样创建一个可观察对象
const final$ = of("completed");
并使用 rxjs concat 运算符,像这样将其与现有的可观察对象连接起来。
const myobs$ = from(rows).pipe(concatMap(row => this.uploadItem(row)));
concat(myobs$, final$).subscribe(response=>{
//your existing code here
if(response=="completed")
{
console.log('all observables completed');
}
});
这里我创建了一个example stackblitz。