FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Async/await by yatoogamii · Pull Request #337 · javascript-tutorial/fr.javascript.info · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .md  (7) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
      • solution.md
      • task.md
      • solution.md
      • task.md
      • solution.md
      • task.md
    • article.md
This file contains hidden or 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,5 +1,5 @@

The notes are below the code:
Les notes sont en dessous du code:

```js run
async function loadJson(url) { // (1)
Expand All @@ -19,15 +19,15 @@ loadJson('no-such-user.json')

Notes:

1. The function `loadJson` becomes `async`.
2. All `.then` inside are replaced with `await`.
3. We can `return response.json()` instead of awaiting for it, like this:
1. La fonction `loadJson` devient `async`.
2. Tous les `.then` intérieurs sont remplacés par `await`..
3. Nous pouvons `return response.json()` au lieu de l'attendre, comme ceci:

```js
if (response.status == 200) {
return response.json(); // (3)
}
```

Then the outer code would have to `await` for that promise to resolve. In our case it doesn't matter.
4. The error thrown from `loadJson` is handled by `.catch`. We can't use `await loadJson()` there, because we're not in an `async` function.
Ensuite, le code externe devra "attendre" la résolution de cette promesse. Dans notre cas, cela n'a pas d'importance.
4. L'erreur émise par `loadJson` est gérée par `.catch`. Nous ne pouvons pas utiliser `await loadJson(...)` ici, car nous ne sommes pas dans une fonction `async`..
This file contains hidden or 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,7 +1,7 @@

# Rewrite using async/await
# Réécriture avec async/await

Rewrite this example code from the chapter <info:promise-chaining> using `async/await` instead of `.then/catch`:
Réécrire cet exemple de code du chapitre <info:promise-chaining> en utilisant `async/await` au lieu de `.then/catch`:

```js run
function loadJson(url) {
Expand Down
This file contains hidden or 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,5 +1,5 @@

There are no tricks here. Just replace `.catch` with `try..catch` inside `demoGithubUser` and add `async/await` where needed:
Il n'y a pas d'astuces ici. Remplacez simplement `.catch` par `try..catch` dans `demoGithubUser` et ajoutez `async/await` là où c'est nécessaire:

```js run
class HttpError extends Error {
Expand All @@ -19,7 +19,7 @@ async function loadJson(url) {
}
}

// Ask for a user name until github returns a valid user
// demander un nom d'utilisateur jusqu'à ce que github renvoie un utilisateur valide
async function demoGithubUser() {

let user;
Expand All @@ -28,13 +28,13 @@ async function demoGithubUser() {

try {
user = await loadJson(`https://api.github.com/users/${name}`);
break; // no error, exit loop
break; // pas d'erreur, sortie de la boucle
} catch(err) {
if (err instanceof HttpError && err.response.status == 404) {
// loop continues after the alert
// la boucle continue après l'alerte
alert("No such user, please reenter.");
} else {
// unknown error, rethrow
// erreur inconnue, rejeter
throw err;
}
}
Expand Down
This file contains hidden or 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,9 +1,9 @@

# Rewrite "rethrow" with async/await
# Réécriture de "rethrow" avec async/await

Below you can find the "rethrow" example. Rewrite it using `async/await` instead of `.then/catch`.
Vous trouverez ci-dessous l'exemple "rethrow". Réécrivez-le en utilisant `async/await` au lieu de `.then/catch`.

And get rid of the recursion in favour of a loop in `demoGithubUser`: with `async/await` that becomes easy to do.
Et débarrassez-vous de la récursion en faveur d'une boucle dans `demoGithubUser` : avec `async/await`, cela devient facile à faire.

```js run
class HttpError extends Error {
Expand All @@ -25,7 +25,7 @@ function loadJson(url) {
});
}

// Ask for a user name until github returns a valid user
// demander un nom d'utilisateur jusqu'à ce que github renvoie un utilisateur valide
function demoGithubUser() {
let name = prompt("Enter a name?", "iliakan");

Expand Down
This file contains hidden or 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,7 +1,7 @@

That's the case when knowing how it works inside is helpful.
C'est le cas quand il est utile de savoir comment ça marche à l'intérieur.

Just treat `async` call as promise and attach `.then` to it:
Il suffit de traiter l'appel `async` comme une promesse et d'y attacher `.then`:
```js run
async function wait() {
await new Promise(resolve => setTimeout(resolve, 1000));
Expand All @@ -10,7 +10,7 @@ async function wait() {
}

function f() {
// shows 10 after 1 second
// affiche 10 après 1 seconde
*!*
wait().then(result => alert(result));
*/!*
Expand Down
This file contains hidden or 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,7 +1,7 @@

# Call async from non-async
# Appeler l'asynchrone à partir du non-asynchrone

We have a "regular" function called `f`. How can you call the `async` function `wait()` and use its result inside of `f`?
Nous avons une fonction "normale" appelée `f`. Comment pouvez-vous appeler la fonction `async` `wait()` et utiliser son résultat à l'intérieur de `f` ?

```js
async function wait() {
Expand All @@ -11,10 +11,10 @@ async function wait() {
}

function f() {
// ...what should you write here?
// we need to call async wait() and wait to get 10
// remember, we can't use "await"
// ...que devez-vous écrire ici?
// nous devons appeler async wait() et attendre pour obtenir 10
// Souvenez-vous, on ne peut pas utiliser "await".
}
```

P.S. The task is technically very simple, but the question is quite common for developers new to async/await.
P.S. La tâche est techniquement très simple, mais la question est assez courante pour les développeurs novices en matière d'async/await.
Loading

Back | FazBrowse Home | New Git URL