Highlighting the use of caching for side effects
The use case we covered in this chapter involved optimizing HTTP requests to enhance our web applications’ performance. All you have to do is put the result in a cache, which acts as a shared place for all consumers.
There are other use cases where caching streams makes a lot of sense, namely when accounting for expensive side effects on the streams. In general, we call the actions that we perform after a value is emitted side effects. This could be logging, displaying messages, doing a mapping, and so on.
Here’s an example of a side effect using the tap
operator:
import {map, from } from 'rxjs'; import { tap } from 'rxjs/operators'; const stream$ = from([1, 2, 'Hello', 5]); stream$ .pipe( tap((value) => console.log(value)), map((element) => { if (isNaN(element as number)) { ...