# finalize / finally

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

## Call a function when observable completes or errors

### Examples

**Example 1: Execute callback function when the observable completes**

( [StackBlitz](https://stackblitz.com/edit/typescript-ohddud) )

```js
import { interval } from 'rxjs';
import { take, finalize } from 'rxjs/operators';

//emit value in sequence every 1 second
const source = interval(1000);
//output: 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));
```

### Related Recipes

* [Battleship Game](/ko.learn-rxjs/rxjs/recipes/battleship-game.md)
* [Car Racing Game](/ko.learn-rxjs/rxjs/recipes/car-racing-game.md)
* [Click Ninja Game](/ko.learn-rxjs/rxjs/recipes/click-ninja-game.md)
* [HTTP Polling](/ko.learn-rxjs/rxjs/recipes/http-polling.md)
* [Mine Sweeper Game](/ko.learn-rxjs/rxjs/recipes/mine-sweeper-game.md)
* [Swipe To Refresh](/ko.learn-rxjs/rxjs/recipes/swipe-to-refresh.md)
* [Tetris Game](/ko.learn-rxjs/rxjs/recipes/tetris-game.md)

### Additional Resources

* [finalize](https://rxjs.dev/api/operators/finalize) 📰 - Official docs

***

> 📁 Source Code: <https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/finalize.ts>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://chasethestar.gitbook.io/ko.learn-rxjs/rxjs/operators/utility/finalize.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
