D
D
dev-executor2017-09-20 15:15:58
JavaScript
dev-executor, 2017-09-20 15:15:58

How to prevent Observable from stopping?

There is such a code, in case of an error, the interval stops, and the observable becomes executed, how to prevent this?

Observable.interval(3000)
            .do(value => console.log('attempt #', value))
            .switchMap(() => this.http.get(url))
            .catch (err => Observable.empty())
            .subscribe(data => console.log(data));

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
davidnum95, 2017-09-21
@dev-executor

You need to create a new Observable inside switchMap that will catch the error:

Observable.interval(3000)
            .do(value => console.log('attempt #', value))
            .switchMap(() => Observable.defer(() => this.http.get(url))
                  .map(data => data)
                  .catch(err => console.log(err))
            )
            .catch (err => Observable.empty())
            .subscribe(data => console.log(data));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question