BACK
Referencefinalize
Reference · 34

finalize

Runs a callback on complete, error, or unsubscribe.

Signature

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

What it does

A guaranteed "finally" for the stream. The callback fires exactly once on any ending: complete, error, or manual unsubscription.

When to use

Hide a loader, release a resource (a port, a file), reset state, send end-of-operation analytics. Pairs with a loading flag: tap(() => loading = true) + finalize(() => loading = false).

Gotcha

finalize gets no access to the value or the error. If you need those, combine it with tap. Also, finalize runs after error/complete, so the subscriber has already been notified.

See also

tap, catchError, takeUntil

script.ts // TypeScript
CONSOLE · Console output
Hit Run to see the result...