Skip to content
Open

Fetch #459

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/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe("getUsers", function() {

it("gets users from GitHub", async function() {
it("Puxar usuário do GitHub", async function() {
let users = await getUsers(['iliakan', 'remy', 'no.such.users']);
assert.equal(users[0].login, 'iliakan');
assert.equal(users[1].login, 'remy');
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')`.
Para buscar um usuário, precisamos de algo assim: `fetch('https://api.github.com/users/USERNAME')`.

If the response has status `200`, call `.json()` to read the JS object.
Se a resposta tiver status `200`, chamamos `.json()` para ler o objeto JS.

Otherwise, if a `fetch` fails, or the response has non-200 status, we just return `null` in the resulting array.
Caso contrário, se o `fetch` falhar ou a resposta tiver um status diferente de 200, simplesmente retornamos `null` na lista (array) resultante.

So here's the code:
Aqui está o código:

```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.
Atenção: Repare que a chamada `.then` está encadeada diretamente no `fetch` — assim, ao receber a resposta, ela não espera pelos outros fetches e já começa a ler o `.json()` imediatamente.

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.
Se usássemos `await Promise.all(names.map(name => fetch(...)))` e chamássemos `.json()` nos resultados depois, teríamos que esperar que todos os fetches respondessem primeiro. Ao encadear `.json()` diretamente em cada `fetch`, garantimos que cada requisição começa a processar os dados como JSON de forma independente, sem esperar pelas demais.

That's an example of how low-level Promise API can still be useful even if we mainly use `async/await`.
Esse é um exemplo de como a API de baixo nível de Promises ainda pode ser muito útil, mesmo quando usamos `async/await` na maior parte do tempo.
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
# Buscar usuários do 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.
Crie uma função assíncrona `getUsers(names)` que receba um array de logins do GitHub, busque os usuários na API do GitHub e retorne um array com os dados desses usuários.

The GitHub url with user information for the given `USERNAME` is: `https://api.github.com/users/USERNAME`.
A URL do GitHub com as informações de um certo `USERNAME` é: `https://api.github.com/users/USERNAME`.

There's a test example in the sandbox.
Há um exemplo de teste no sandbox.

Important details:
Detalhes importantes:

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. Deve haver uma requisição `fetch` por usuário.
2. As requisições não devem esperar umas pelas outras — assim os dados chegam o mais rápido possível.
3. Se alguma requisição falhar, ou se o usuário não existir, a função deve retornar `null` na posição correspondente do array.
Loading