|
1 | 1 |
|
2 |
| -To fetch a user we need: `fetch('https://api.github.com/users/USERNAME')`. |
| 2 | +Pour récupérer un utilisateur, nous avons besoin de : `fetch('https://api.github.com/users/USERNAME')`. |
3 | 3 |
|
4 |
| -If the response has status `200`, call `.json()` to read the JS object. |
| 4 | +Si la réponse a le statut `200`, appelons `.json()` pour lire l'objet JS. |
5 | 5 |
|
6 |
| -Otherwise, if a `fetch` fails, or the response has non-200 status, we just return `null` in the resulting arrray. |
| 6 | +Sinon, si un `fetch` échoue, ou si la réponse a un statut différent de 200, nous renvoyons simplement `null` dans le tableau de résutats. |
7 | 7 |
|
8 |
| -So here's the code: |
| 8 | +Voici donc le code : |
9 | 9 |
|
10 | 10 | ```js demo
|
11 | 11 | async function getUsers(names) {
|
@@ -33,8 +33,8 @@ async function getUsers(names) {
|
33 | 33 | }
|
34 | 34 | ```
|
35 | 35 |
|
36 |
| -Please note: `.then` call is attached directly to `fetch`, so that when we have the response, it doesn't wait for other fetches, but starts to read `.json()` immediately. |
| 36 | +Veuillez noter : l'appel `.then` est directement attaché à `fetch`, de sorte que lorsque nous avons la réponse, il n'attend pas d'autres fetches, mais commence à lire `.json()` immédiatement. |
37 | 37 |
|
38 |
| -If we used `await Promise.all(names.map(name => fetch(...)))`, and call `.json()` on the results, then it would wait for all fetches to respond. By adding `.json()` directly to each `fetch`, we ensure that individual fetches start reading data as JSON without waiting for each other. |
| 38 | +Si nous avions utilisé `await Promise.all(names.map(name => fetch(...)))`, et appelé `.json()` sur les résultats, il aurait attendu que tous les fetches répondent. En ajoutant `.json()` directement à chaque `fetch`, nous nous assurons que les fetches individuels commencent à lire les données en JSON sans s'attendre les uns les autres. |
39 | 39 |
|
40 |
| -That's an example of how low-level Promise API can still be useful even if we mainly use `async/await`. |
| 40 | +C'est un exemple de la façon dont l'API Promise de bas niveau peut toujours être utile même si nous utilisons principalement `async/wait`. |
0 commit comments