rxjsobservableconcatmap

ConcatMap interupted when error in one observable


I have the following code:

Observable.from(modifiedNodes)
    .concatMap(node => {
        return this.Model.setData(node);
    })
    .subscribe(() => {
        NodeSaved++;
    }

If setData throws an error for any intermediate node, the next sequence of nodes are not getting resumed. How we can modify it so that even if there is an error for one node, the next sequence of nodes will be executed? I read about onErrorResumeNext but I'm not sure how to use it here.

The method signature for setData is as follows:

public setData(node: INode): Observable<Object>{}

Solution

  • If setData returns an observable, you can use the catch operator. It will receive any errors thrown from within setData and if you return an empty observable, concatMap will move to the next node in the sequence:

    import 'rxjs/add/observable/empty';
    import 'rxjs/add/operator/catch';
    
    Observable
      .from(modifiedNodes)
      .concatMap(node => this.Model
        .setData(node)
        .catch(error => Observable.empty())
      )
      .subscribe(() => { NodeSaved++; });