Learn RxJS
Search…
endWith

signature: endWith(an: Values): Observable

Emit given value(s) on completion.

πŸ’‘ If you want to start with a value instead, check out startWith!
πŸ’‘ If you want to perform an action on completion, but do not want to emit a value, check out finalize!

Examples

Example 1: Basic endWith example
1
// RxJS v6+
2
import { endWith } from 'rxjs/operators';
3
import { of } from 'rxjs';
4
​
5
const source$ = of('Hello', 'Friend', 'Goodbye');
6
​
7
source$
8
// emit on completion
9
.pipe(endWith('Friend'))
10
// 'Hello', 'Friend', 'Goodbye', 'Friend'
11
.subscribe(console.log(val));
Copied!
Example 2: endWith multiple values
1
// RxJS v6+
2
import { endWith } from 'rxjs/operators';
3
import { of } from 'rxjs';
4
​
5
const source$ = of('Hello', 'Friend');
6
​
7
source$
8
// emit on completion
9
.pipe(endWith('Goodbye', 'Friend'))
10
// 'Hello', 'Friend', 'Goodbye', 'Friend'
11
.subscribe(console.log(val));
Copied!
Example 3: Comparison to finalize
1
// RxJS v6+
2
import { endWith, finalize } from 'rxjs/operators';
3
import { of } from 'rxjs';
4
​
5
const source$ = of('Hello', 'Friend');
6
​
7
source$
8
// emit on completion
9
.pipe(
10
endWith('Goodbye', 'Friend'),
11
// this function is invoked when unsubscribe methods are called
12
finalize(() => console.log('Finally'))
13
)
14
// 'Hello', 'Friend', 'Goodbye', 'Friend'
15
.subscribe(val => console.log(val));
16
// 'Finally'
Copied!

Additional Resources

  • ​endWith πŸ“° - Official docs
Last modified 1yr ago