# finalize

<details>

<summary>Signatura</summary>

#### Firma

`finalize<T>(callback: () => void): MonoTypeOperatorFunction<T>`

#### Parameters

#### Retorna

`MonoTypeOperatorFunction<T>`: Un Observable que refleja la fuente, pero que hará una llamada a la función proporcionada tras la terminación de la fuente.

### Ejemplos

**Ejecutar la función&#x20;*****callback*****&#x20;tras la compleción del Observable**

[StackBlitz](https://stackblitz.com/edit/rxjs-finalize-1?file=index.ts)

```javascript
import { of } from "rxjs";
import { finalize } from "rxjs/operators";

const fruit$ = of("Cereza", "Fresa", "Arándano");

fruit$
  .pipe(finalize(() => console.log("Ejecutando finalize")))
  .subscribe(console.log, console.error, () => console.log("Flujo completado"));
// Salida: Cereza, Fresa, Arándano, Flujo completado, Ejecutando finalize
```

`finalize`

**La función&#x20;*****callback*****&#x20;se ejecuta aunque ocurra un error**

[StackBlitz](https://stackblitz.com/edit/rxjs-finalize-2?file=index.ts)

```javascript
import { throwError } from "rxjs";
import { finalize } from "rxjs/operators";

const error$ = throwError("¡Oh no!");

error$
  .pipe(finalize(() => console.log("Ejecutando a pesar del error!")))
  .subscribe(console.log, console.error);
// Output: (error) '¡Oh no!', 'Ejecutando a pesar del error!'
```

**Ejecutar la función&#x20;*****callback*****&#x20;tras realizar todas las peticiones AJAX**

[StackBlitz](https://stackblitz.com/edit/rxjs-finalize-3?file=index.ts)

```javascript
import { finalize, take, map, mergeAll } from "rxjs/operators";
import { ajax } from "rxjs/ajax";

const ghibliFilm$ = ajax.getJSON(`https://ghibliapi.herokuapp.com/films/`);

ghibliFilm$
  .pipe(
    mergeAll(),
    map(({ title }) => title),
    take(3),
    finalize(() => console.log("Peticiones Realizadas"))
  )
  .subscribe(console.log, console.error, () => console.log("Flujo Completado"));
// Salida: Castle in the Sky, Grave of the Fireflies, My Neighbor Totoro, Flujo Completado, Peticiones Realizadas
```

### Recursos adicionales

[![Source code](https://github.com/puntotech/rxjs-docu/blob/master/doc/operators/utility/assets/icons/source-code.png)](https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/finalize.ts)

[Documentación oficial en inglés](https://rxjs.dev/api/operators/finalize)

</details>


---

# 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://www.rxjs.es/operadores/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.
