Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(finalize): examples, improvement #4578 #5065

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/internal/operators/finalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,43 @@ import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
/**
* Returns an Observable that mirrors the source Observable, but will call a specified function when
* the source terminates on complete or error.
* The specified function will also be called when the subscriber explicitly unsubscribes.
*
* ## Examples
* Execute callback function when the observable completes
*
* ```ts
* import { interval } from 'rxjs';
* import { take, finalize } from 'rxjs/operators';
*
* // emit value in sequence every 1 second
* const source = interval(1000);
* // 0,1,2,3,4,5....
* const example = source.pipe(
* take(5), //take only the first 5 values
* finalize(() => console.log('Sequence complete')) // Execute when the observable completes
* )
* const subscribe = example.subscribe(val => console.log(val));
* // 0,1,2,3,4,Sequence complete
* ```
*
* Execute callback function when the subscriber explicitly unsubscribes
*
* ```ts
* import { interval, timer, noop } from 'rxjs';
* import { finalize, tap } from 'rxjs/operators';
*
* const source = interval(100).pipe(
* finalize(() => console.log('[finalize] Called')),
* tap(noop, noop, () => console.log('[tap] Not called')),
* );
*
* const sub = source.subscribe(x => console.log(x), noop, () => console.log('[complete] Not called'));
*
* timer(150).subscribe(() => sub.unsubscribe());
* // 0,[finalize] Called
* ```
*
* @param {function} callback Function to be called when source terminates.
* @return {Observable} An Observable that mirrors the source, but will call the specified function on termination.
* @method finally
Expand Down