endWith
💡 If you want to perform an action on completion, but do not want to emit a value, check out
finalize
!Example 1: Basic endWith example
// RxJS v6+
import { endWith } from 'rxjs/operators';
import { of } from 'rxjs';
const source$ = of('Hello', 'Friend', 'Goodbye');
source$
// emit on completion
.pipe(endWith('Friend'))
// 'Hello', 'Friend', 'Goodbye', 'Friend'
.subscribe(console.log(val));
Example 2: endWith multiple values
// RxJS v6+
import { endWith } from 'rxjs/operators';
import { of } from 'rxjs';
const source$ = of('Hello', 'Friend');
source$
// emit on completion
.pipe(endWith('Goodbye', 'Friend'))
// 'Hello', 'Friend', 'Goodbye', 'Friend'
.subscribe(console.log(val));
Example 3: Comparison to finalize
// RxJS v6+
import { endWith, finalize } from 'rxjs/operators';
import { of } from 'rxjs';
const source$ = of('Hello', 'Friend');
source$
// emit on completion
.pipe(
endWith('Goodbye', 'Friend'),
// this function is invoked when unsubscribe methods are called
finalize(() => console.log('Finally'))
)
// 'Hello', 'Friend', 'Goodbye', 'Friend'
.subscribe(val => console.log(val));
// 'Finally'
Last modified 1yr ago