> ## Documentation Index
> Fetch the complete documentation index at: https://docs.youka.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Tâches

> Inspectez les tâches et attendez la fin des opérations asynchrones

Chaque opération asynchrone dans Youka s’exécute finalement sous forme de tâche, mais la surface principale du SDK renvoie désormais des handles d’opération plutôt que des identifiants de tâche bruts. La plupart des utilisateurs devraient utiliser `client.projects.wait(...)` ou `client.exports.wait(...)` et ne recourir à `client.tasks.*` que lorsqu’ils ont besoin d’une inspection bas niveau des tâches.

## `client.tasks.get(taskId, options?)`

Récupère l’état actuel d’une tâche par ID.

```ts theme={null}
const task = await client.tasks.get("tsk_abc123");
console.log(task.status, task.type);
```

### Statuts de tâche

| Statut        | Terminal ? | Signification                                                        |
| ------------- | ---------- | -------------------------------------------------------------------- |
| `created`     | Non        | La tâche a été créée mais n’a pas encore été mise en file d’attente. |
| `queued`      | Non        | La tâche attend de s’exécuter.                                       |
| `in-progress` | Non        | La tâche est en cours d’exécution.                                   |
| `completed`   | Oui        | La tâche s’est terminée avec succès.                                 |
| `finalized`   | Oui        | La tâche est terminée et le post-traitement est effectué.            |
| `failed`      | Oui        | La tâche a échoué avec une erreur.                                   |
| `cancelled`   | Oui        | La tâche a été annulée.                                              |
| `timed-out`   | Oui        | La tâche a atteint sa limite de temps.                               |

## `client.tasks.wait(taskId, options?)`

Interroge une tâche jusqu’à ce qu’elle atteigne un état terminal. Renvoie la tâche finale en cas de succès et lève `YoukaTaskError` en cas d’échec.

```ts theme={null}
const task = await client.tasks.wait("tsk_abc123", {
  pollIntervalMs: 3_000,
  signal: abortController.signal,
});
```

### Options

<ParamField path="pollIntervalMs" type="number">
  Millisecondes entre deux interrogations. Par défaut : `2000`.
</ParamField>

<ParamField path="signal" type="AbortSignal">
  Annule l’attente. La requête en cours et tout délai en attente sont annulés
  immédiatement.
</ParamField>

### Erreurs

`client.tasks.wait(...)` lève `YoukaTaskError` lorsque la tâche se termine en `failed`, `cancelled` ou `timed-out` :

```ts theme={null}
import { YoukaTaskError } from "@youka/sdk";

try {
  await client.tasks.wait("tsk_abc123");
} catch (error) {
  if (error instanceof YoukaTaskError) {
    console.error(error.code); // TASK_FAILED | TASK_CANCELLED | TASK_TIMED_OUT
    console.error(error.task); // Full RestTask payload
  }
  throw error;
}
```

## `client.projects.wait(operation, options?)`

Attend la fin d’une opération à l’échelle d’un projet, puis récupère à nouveau le projet. Renvoie le handle d’opération, la tâche terminale et le projet mis à jour.

```ts theme={null}
const operation = await client.projects.create({
  source: { type: "path", path: "./song.mp3" },
});

const { project, task } = await client.projects.wait(operation, {
  pollIntervalMs: 2_500,
});

console.log("Project ready:", project.id);
console.log("Task finished in state:", task.status);
```

<ParamField path="operation" type="ProjectOperation" required>
  Généralement le résultat de `client.projects.create(...)`,
  `client.projects.separateStems(...)` ou `client.projects.syncLyrics(...)`.
</ParamField>

## `client.exports.wait(operationOrId, options?)`

Attend qu’un export cloud atteigne un état terminal. Passez soit l’`ExportOperation` renvoyée par `client.exports.create(...)`, soit une chaîne `exportId`.

```ts theme={null}
const operation = await client.exports.create("prj_abc123", {
  resolution: "1080p",
  quality: "high",
});

const finalized = await client.exports.wait(operation, {
  pollIntervalMs: 3_000,
});

console.log(finalized.status, finalized.url);
```

## Annulation

Passez un `AbortSignal` pour annuler une attente longue :

```ts theme={null}
const controller = new AbortController();

setTimeout(() => controller.abort(), 60_000);

try {
  await client.projects.wait(operation, { signal: controller.signal });
} catch (error) {
  if (error instanceof Error && error.name === "AbortError") {
    console.log("Wait cancelled after 60s");
  } else {
    throw error;
  }
}
```

Le même signal annule aussi la requête d’interrogation sous-jacente de la tâche ou de l’export.

## Et ensuite

* [Erreurs](/fr/sdk/errors) — gérer `YoukaTaskError` et les erreurs pouvant être retentées
* [Exports](/fr/sdk/exports) — attendre la fin d’un export
* [Tâches asynchrones de l’API](/fr/api/async-jobs) — le même modèle en HTTP brut
