Skip to content

Commit b2140a8

Browse files
committed
Update 5-network folder
2 parents 81123a7 + b617cc1 commit b2140a8

File tree

57 files changed

+24821
-9
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+24821
-9
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
async function getUsers(names) {
3+
let jobs = [];
4+
5+
for(let name of names) {
6+
let job = fetch(`https://api.github.com/users/${name}`).then(
7+
successResponse => {
8+
if (successResponse.status != 200) {
9+
return null;
10+
} else {
11+
return successResponse.json();
12+
}
13+
},
14+
failResponse => {
15+
return null;
16+
}
17+
);
18+
jobs.push(job);
19+
}
20+
21+
let results = await Promise.all(jobs);
22+
23+
return results;
24+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
async function getUsers(names) {
3+
/* your code */
4+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
describe("getUsers", function() {
2+
3+
it("gets users from GitHub", async function() {
4+
let users = await getUsers(['iliakan', 'remy', 'no.such.users']);
5+
assert.equal(users[0].login, 'iliakan');
6+
assert.equal(users[1].login, 'remy');
7+
assert.equal(users[2], null);
8+
});
9+
10+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
To fetch a user we need: `fetch('https://api.github.com/users/USERNAME')`.
3+
4+
If the response has status `200`, call `.json()` to read the JS object.
5+
6+
Otherwise, if a `fetch` fails, or the response has non-200 status, we just return `null` in the resulting arrray.
7+
8+
So here's the code:
9+
10+
```js demo
11+
async function getUsers(names) {
12+
let jobs = [];
13+
14+
for(let name of names) {
15+
let job = fetch(`https://api.github.com/users/${name}`).then(
16+
successResponse => {
17+
if (successResponse.status != 200) {
18+
return null;
19+
} else {
20+
return successResponse.json();
21+
}
22+
},
23+
failResponse => {
24+
return null;
25+
}
26+
);
27+
jobs.push(job);
28+
}
29+
30+
let results = await Promise.all(jobs);
31+
32+
return results;
33+
}
34+
```
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.
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.
39+
40+
That's an example of how low-level Promise API can still be useful even if we mainly use `async/await`.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Fetch users from GitHub
2+
3+
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.
4+
5+
The GitHub url with user information for the given `USERNAME` is: `https://api.github.com/users/USERNAME`.
6+
7+
There's a test example in the sandbox.
8+
9+
Important details:
10+
11+
1. There should be one `fetch` request per user.
12+
2. Requests shouldn't wait for each other. So that the data arrives as soon as possible.
13+
3. If any request fails, or if there's no such user, the function should return `null` in the resulting array.

0 commit comments

Comments
 (0)