Skip to content

Fetch #155

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 4 commits into from
Dec 8, 2022
Merged

Fetch #155

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
2 changes: 1 addition & 1 deletion 5-network/01-fetch/01-fetch-users/_js.view/source.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

async function getUsers(names) {
/* your code */
/* codul tău */
}
4 changes: 2 additions & 2 deletions 5-network/01-fetch/01-fetch-users/_js.view/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe("getUsers", function() {

it("gets users from GitHub", async function() {
let users = await getUsers(['iliakan', 'remy', 'no.such.users']);
it("obține utilizatorii din GitHub", async function() {
let users = await getUsers(['iliakan', 'remy', 'nici.un.astfel.de.utilizator']);
assert.equal(users[0].login, 'iliakan');
assert.equal(users[1].login, 'remy');
assert.equal(users[2], null);
Expand Down
14 changes: 7 additions & 7 deletions 5-network/01-fetch/01-fetch-users/solution.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

To fetch a user we need: `fetch('https://api.github.com/users/USERNAME')`.
Pentru a prelua un utilizator avem nevoie de: `fetch('https://api.github.com/users/USERNAME')`.

If the response has status `200`, call `.json()` to read the JS object.
Dacă răspunsul are status `200`, apelați `.json()` pentru a citi obiectul JS.

Otherwise, if a `fetch` fails, or the response has non-200 status, we just return `null` in the resulting array.
În caz contrar, dacă un `fetch` eșuează, sau dacă răspunsul nu are status 200, returnăm doar `null` în array-ul rezultat.

So here's the code:
Iată deci codul:

```js demo
async function getUsers(names) {
Expand Down Expand Up @@ -33,8 +33,8 @@ async function getUsers(names) {
}
```

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.
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()`.

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.
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.

That's an example of how low-level Promise API can still be useful even if we mainly use `async/await`.
Acesta este un exemplu despre cum low-level Promise API poate fi totuși util chiar dacă folosim în principal `async/await`.
16 changes: 8 additions & 8 deletions 5-network/01-fetch/01-fetch-users/task.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Fetch users from GitHub
# Preia utilizatorii din GitHub

Create an async function `getUsers(names)`, that gets an array of GitHub logins, fetches the users from GitHub and returns an array of GitHub users.
Creați o funcție async `getUsers(names)`, care obține un array de autentificări GitHub, preia utilizatorii de pe GitHub și returnează un array de utilizatori GitHub.

The GitHub url with user information for the given `USERNAME` is: `https://api.github.com/users/USERNAME`.
URL-ul GitHub cu informații despre utilizator pentru `USERNAME` dat este: `https://api.github.com/users/USERNAME`.

There's a test example in the sandbox.
Există un exemplu de testare în sandbox.

Important details:
Detalii importante:

1. There should be one `fetch` request per user.
2. Requests shouldn't wait for each other. So that the data arrives as soon as possible.
3. If any request fails, or if there's no such user, the function should return `null` in the resulting array.
1. Trebuie să existe o singură cerere `fetch` pentru fiecare utilizator.
2. Solicitările nu ar trebui să se aștepte una pe alta. Astfel încât datele să ajungă cât mai repede posibil.
3. În cazul în care o cerere eșuează, sau dacă nu există un astfel de utilizator, funcția ar trebui să returneze `null` în array-ul rezultat.
Loading