【问题标题】:Subscribe to final observable after all observable completed?在所有可观察对象完成后订阅最终可观察对象?
【发布时间】:2020-11-07 16:37:03
【问题描述】:

我这里有这个逻辑,它创建了许多 observable 并在每个完成后执行回调。

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 调用

那会去哪里?

【问题讨论】:

    标签: angular angular9 angular10 rxjs-observables


    【解决方案1】:

    你可以像这样创建一个 observable

    const final$ = of("completed");
    

    并使用 rxjs concat 运算符,将其与现有的 observable 像这样连接。

    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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-09
      • 2022-01-23
      • 1970-01-01
      • 2019-04-23
      相关资源
      最近更新 更多