do / tap
signature: do(nextOrObserver: function, error: function, complete: function): Observable
Transparently perform actions or side-effects, such as logging.
If you are using as a pipeable operator, do
is known as tap
!
Examples
Example 1: Logging with do
( StackBlitz | jsBin | jsFiddle )
// RxJS v6+
import { of } from 'rxjs';
import { tap, map } from 'rxjs/operators';
const source = of(1, 2, 3, 4, 5);
//transparently log values from source with 'do'
const example = source.pipe(
tap(val => console.log(`BEFORE MAP: ${val}`)),
map(val => val + 10),
tap(val => console.log(`AFTER MAP: ${val}`))
);
//'do' does not transform values
//output: 11...12...13...14...15
const subscribe = example.subscribe(val => console.log(val));
Additional Resources
- do - Official docs
- Logging a stream with do - John Linquist
- Utility operator: do - André Staltz
Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/do.ts