Skip to content

Commit

Permalink
fr: tutorial/01/04/06
Browse files Browse the repository at this point in the history
  • Loading branch information
bleucitron committed Jan 20, 2025
1 parent 00c5022 commit 11d7465
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</script>

<button onclick={() => promise = roll()}>
roll the dice
lancer les dés
</button>

<p>...rolling</p>
<p>...les dés roulent</p>
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
export async function roll() {
// Fetch a random number from 1 to 6
// (with a delay, so that we can see it)
return new Promise((fulfil, reject) => {
setTimeout(() => {
// simulate a flaky network
if (Math.random() < 0.3) {
reject(new Error('Request failed'));
return;
}
// Récupère un nombre aléatoire entre 1 et 6
// (avec un retard, pour pouvoir s'en rendre compte)
return new Promise((fulfil, reject) => {
setTimeout(() => {
// simule un réseau instable
if (Math.random() < 0.3) {
reject(new Error('La requête a échoué'));
return;
}

fulfil(Math.ceil(Math.random() * 6));
}, 1000);
});
fulfil(Math.ceil(Math.random() * 6));
}, 1000);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
</script>

<button onclick={() => promise = roll()}>
roll the dice
lancer les dés
</button>

{#await promise}
<p>...rolling</p>
<p>...les dés roulent</p>
{:then number}
<p>you rolled a {number}!</p>
<p>vous avez obtenu un {number} !</p>
{:catch error}
<p style="color: red">{error.message}</p>
{/await}
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
---
title: Await blocks
title: Blocs await
---

Most web applications have to deal with asynchronous data at some point. Svelte makes it easy to _await_ the value of [promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises) directly in your markup:
La plupart des applications web doivent gérer des données asynchrones à un moment ou à un autre.
Svelte permet de facilement attendre (_await_) la valeur des
[promesses](https://developer.mozilla.org/fr/docs/Web/JavaScript/Guide/Using_promises)
directement dans votre markup :

```svelte
/// file: App.svelte
+++{#await promise}+++
<p>...rolling</p>
<p>...les dés roulent</p>
+++{:then number}
<p>you rolled a {number}!</p>
<p>vous avez obtenu un {number} !</p>
{:catch error}
<p style="color: red">{error.message}</p>
{/await}+++
```

> [!NOTE] Only the most recent `promise` is considered, meaning you don't need to worry about race conditions.
> [!NOTE] Seule la `promise` la plus récente est traitée, ce qui signifie que vous n'avez pas besoin
> de vous préoccuper d'éventuelles race conditions.
If you know that your promise can't reject, you can omit the `catch` block. You can also omit the first block if you don't want to show anything until the promise resolves:
Si vous savez que votre promesse ne peut pas être rejetée, vous pouvez omettre le bloc `catch`. Vous
pouvez également omettre le premier bloc si vous ne souhaitez rien afficher tant que la promesse
n'est pas résolue :

```svelte
{#await promise then number}
<p>you rolled a {number}!</p>
<p>vous avez obtenu un {number} !</p>
{/await}
```

0 comments on commit 11d7465

Please sign in to comment.