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);
|
||||
});
|
||||
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue