Search code examples
angularangular9angular10rxjs-observables

Subscribe to final observable after all observable completed?


I have this logic here, which creates many observable and executes a call back after each of them is done.

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

My issue here is that I'd like to be able to subscribe to something that would tell me uploadItem has been called for each of the rows

Where would that go ?


Solution

  • You can create an observable like this

    const final$ = of("completed");
    

    And using rxjs concat operator, concat it with the existing observable like this.

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

    Here I created an example stackblitz.