Skip to content

Async/await #337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions 1-js/11-async/08-async-await/01-rewrite-async/solution.md
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`..
4 changes: 2 additions & 2 deletions 1-js/11-async/08-async-await/01-rewrite-async/task.md
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
10 changes: 5 additions & 5 deletions 1-js/11-async/08-async-await/02-rewrite-async-2/solution.md
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
8 changes: 4 additions & 4 deletions 1-js/11-async/08-async-await/02-rewrite-async-2/task.md
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
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
12 changes: 6 additions & 6 deletions 1-js/11-async/08-async-await/03-async-from-regular/task.md
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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy