Bart van den Burg
Bart van den Burg

Reputation: 2364

Rxjs subscribe after catch is not called

I have the following lines of code:

this.initializer.initialize()
    .catch((e) => { console.log('catch'); return Observable.empty() })
    .flatMap(() => { console.log('flatmap'); return this.route.params })
    .subscribe(() => { console.log('subscribe'); })

The logged output is

catch

What could be causing the flatMap, and consequently the subscribe to not be called? Not sure what other code could be relevant here, if anything else is needed, I will supply it.

Upvotes: 1

Views: 297

Answers (1)

martin
martin

Reputation: 96959

This is obviously caused by this.initializer.initialize() emitting an error notification (is this intended?).

The catch operator reacts only to error notifications, not next neither complete.

The flatMap() operator reacts only to next notifications, not error neither complete.

The subscribe call as you have it right now only handles next notifications. You could rewrite it to handle also errors:

.subscribe(
  () => console.log('subscribe'),
  err => console.log('error:', err), 
)

However you're using catch and you basically replace the error with Observable.empty() so it never reaches the subscriber so the error handler won't be called anyway.

Upvotes: 2

Related Questions