Learn RxJS
Search…
finalize / finally

signature: finalize(callback: () => void)

Call a function when observable completes or errors

Examples

Example 1: Execute callback function when the observable completes
1
import { interval } from 'rxjs';
2
import { take, finalize } from 'rxjs/operators';
3
4
//emit value in sequence every 1 second
5
const source = interval(1000);
6
//output: 0,1,2,3,4,5....
7
const example = source.pipe(
8
take(5), //take only the first 5 values
9
finalize(() => console.log('Sequence complete')) // Execute when the observable completes
10
)
11
const subscribe = example.subscribe(val => console.log(val));
Copied!

Additional Resources

Last modified 2yr ago