DEV Community

Discussion on: Ever Realized You've Been Doing Things the Hard Way? What Simple Tricks Did You Miss?

Collapse
 
rensjaspers profile image
Rens Jaspers

I'll start.

For years, whenever I needed to handle an error in an RxJS stream with a side effect, I would catch the error and then rethrow it using catchError and throwError. Instead, I could have simply used tap:

stream$.pipe(
  tap( { error: (e) => { alert(e.message); } } )
)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
rensjaspers profile image
Rens Jaspers

Another RxJS one:

I often combine streams with forkJoin or combineLatest and want the output as an object with stream names as keys. Before, I did it like this:

combineLatest([firstStream$, secondStream$]).pipe(
  map(([first, second]) => ({first, second}))
)
// output: { first: 'some value', second: 'some other value' }
Enter fullscreen mode Exit fullscreen mode

But, you can directly do:

combineLatest({ first: firstStream$, second: secondStream$ })
Enter fullscreen mode Exit fullscreen mode