代码之家  ›  专栏  ›  技术社区  ›  Francis Ducharme

在所有可观察项完成后订阅最终可观察项?

  •  0
  • Francis Ducharme  · 技术社区  · 3 年前

    我这里有这样的逻辑,它创建了许多可观察对象,并在每个对象完成后执行回调。

    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

    那会去哪里?

    0 回复  |  直到 3 年前
        1
  •  1
  •   Mujadid Mughal    3 年前

    您可以创建这样的可观察对象

    const final$ = of("completed");
    

    使用rxjs 海螺 操作符,将其与现有的可观测值连接起来,如下所示。

    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 .

    推荐文章