Promise.allSettled
This commit is contained in:
parent
027933531e
commit
d7f90176c7
16 changed files with 191 additions and 253 deletions
|
@ -0,0 +1,24 @@
|
|||
|
||||
async function getUsers(names) {
|
||||
let jobs = [];
|
||||
|
||||
for(let name of names) {
|
||||
let job = fetch(`https://api.github.com/users/${name}`).then(
|
||||
successResponse => {
|
||||
if (successResponse.status != 200) {
|
||||
return null;
|
||||
} else {
|
||||
return successResponse.json();
|
||||
}
|
||||
},
|
||||
failResponse => {
|
||||
return null;
|
||||
}
|
||||
);
|
||||
jobs.push(job);
|
||||
}
|
||||
|
||||
let results = await Promise.all(jobs);
|
||||
|
||||
return results;
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
|
||||
async function getUsers(names) {
|
||||
/* your code */
|
||||
}
|
10
5-network/01-fetch-basics/01-fetch-users/_js.view/test.js
Normal file
10
5-network/01-fetch-basics/01-fetch-users/_js.view/test.js
Normal file
|
@ -0,0 +1,10 @@
|
|||
describe("getUsers", function() {
|
||||
|
||||
it("gets users from GitHub", async function() {
|
||||
let users = getUsers(['iliakan', 'remy', 'no.such.users']);
|
||||
assert.equal(users[0].login, 'iliakan');
|
||||
assert.equal(users[1].login, 'remy');
|
||||
assert.equal(users[2], null);
|
||||
});
|
||||
|
||||
});
|
41
5-network/01-fetch-basics/01-fetch-users/solution.md
Normal file
41
5-network/01-fetch-basics/01-fetch-users/solution.md
Normal file
|
@ -0,0 +1,41 @@
|
|||
|
||||
To fetch a user we need:
|
||||
|
||||
1. `fetch('https://api.github.com/users/USERNAME')`.
|
||||
2. If the response has status `200`, call `.json()` to read the JS object.
|
||||
|
||||
If a `fetch` fails, or the response has non-200 status, we just return `null` in the resulting arrray.
|
||||
|
||||
So here's the code:
|
||||
|
||||
```js
|
||||
async function getUsers(names) {
|
||||
let jobs = [];
|
||||
|
||||
for(let name of names) {
|
||||
let job = fetch(`https://api.github.com/users/${name}`).then(
|
||||
successResponse => {
|
||||
if (successResponse.status != 200) {
|
||||
return null;
|
||||
} else {
|
||||
return successResponse.json();
|
||||
}
|
||||
},
|
||||
failResponse => {
|
||||
return null;
|
||||
}
|
||||
);
|
||||
jobs.push(job);
|
||||
}
|
||||
|
||||
let results = await Promise.all(jobs);
|
||||
|
||||
return results;
|
||||
}
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
That's an example of how low-level `Promise` API can still be useful even if we mainly use `async/await`.
|
12
5-network/01-fetch-basics/01-fetch-users/task.md
Normal file
12
5-network/01-fetch-basics/01-fetch-users/task.md
Normal file
|
@ -0,0 +1,12 @@
|
|||
# Fetch users from GitHub
|
||||
|
||||
Create an async function `getUsers(names)`, that gets an array of GitHub user names, fetches them from GitHub and returns an array of GitHub users instead.
|
||||
|
||||
The GitHub url with user informaiton is: `https://api.github.com/users/USERNAME`.
|
||||
|
||||
There's a test example in the sandbox.
|
||||
|
||||
Important details:
|
||||
|
||||
1. There should be one `fetch` request per user. And requests shouldn't wait for each other. So that the data arrives as soon as possible.
|
||||
2. If a request fails, or if there's no such user, the function should return `null` in the resulting array.
|
Loading…
Add table
Add a link
Reference in a new issue