|
1 | 1 |
|
2 |
| -To fetch a user we need: `fetch('https://api.github.com/users/USERNAME')`. |
| 2 | +Pentru a prelua un utilizator avem nevoie 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 | +Dacă răspunsul are status `200`, apelați `.json()` pentru a citi obiectul JS. |
5 | 5 |
|
6 |
| -Otherwise, if a `fetch` fails, or the response has non-200 status, we just return `null` in the resulting array. |
| 6 | +În caz contrar, dacă un `fetch` eșuează, sau dacă răspunsul nu are status 200, returnăm doar `null` în array-ul rezultat. |
7 | 7 |
|
8 |
| -So here's the code: |
| 8 | +Iată deci codul: |
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 | +Vă rugăm să notați că: apelul `.then` este atașat direct la `fetch`, astfel încât atunci când avem răspunsul, acesta nu așteaptă alte preluări, ci începe să citească imediat `.json()`. |
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 | +Dacă am folosi `await Promise.all(names.map(name => fetch(...)))`, și am apela `.json()` către rezultate, atunci am aștepta ca toate preluările să răspundă. Adăugând `.json()` direct la fiecare `fetch`, ne asigurăm că fiecare fetch individual începe să citească datele ca JSON fără să se aștepte unul pe celălalt. |
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 | +Acesta este un exemplu despre cum low-level Promise API poate fi totuși util chiar dacă folosim în principal `async/await`. |
0 commit comments