forked from sveltejs/svelte.dev
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
00c5022
commit 11d7465
Showing
4 changed files
with
30 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 12 additions & 12 deletions
24
...te.dev/content/tutorial/01-svelte/04-logic/06-await-blocks/+assets/app-a/src/lib/utils.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 13 additions & 7 deletions
20
apps/svelte.dev/content/tutorial/01-svelte/04-logic/06-await-blocks/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
``` |