# Subscribe

Dans l'exemple ci-dessous nous allons créer un observable à l'aide de la fonction `interval` qui produit une **valeur auto-incrémentée de façon régulière**.&#x20;

```typescript
import { interval } from 'rxjs';

const data$ = interval(1000);
```

Tant qu'on ne s'inscrit pas à l'"observable", il ne se passe rien car cet observable est "lazy“.

{% hint style="success" %}
La convention de nommage est de suffixer les "observables" avec `$`.
{% endhint %}

## `Observable.subscribe`

Le point commun entre tous les "observables" est la méthode `subscribe` qui permet de **souscrire à un `Observable`** et être **notifié des nouvelles valeurs**, des **erreurs** ou de la **fin du "stream"**.

```typescript
import { interval } from 'rxjs';
​
const data$ = interval(1000);

data$.subscribe(value => console.log(value));
```

![Résultat du \`subscribe\`](/files/-LC_GP2XkiHBUp6NL56F)

Nous pouvons ensuite ajouter les "callbacks" de capture d'"erreur" ou de fin du "stream" en passant un objet à la méthode subscribe avec les trois callbacks suivantes : `next`, `error` et `complete`.

```typescript
data$.subscribe({
    next: value => console.log(value),
    error: err => console.error(err),
    complete: () => console.log('DONE!')
});
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://guide-angular.wishtack.io/angular/observables/subscribe.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.
