up
This commit is contained in:
parent
06d7123df1
commit
3113f7deef
4 changed files with 40 additions and 0 deletions
49
1-js/11-async/07-async-await/02-rewrite-async-2/solution.md
Normal file
49
1-js/11-async/07-async-await/02-rewrite-async-2/solution.md
Normal file
|
@ -0,0 +1,49 @@
|
|||
|
||||
There are no tricks here. Just replace `.catch` with `try...catch` inside `demoGithubUser` and add `async/await` where needed:
|
||||
|
||||
```js run
|
||||
class HttpError extends Error {
|
||||
constructor(response) {
|
||||
super(`${response.status} for ${response.url}`);
|
||||
this.name = 'HttpError';
|
||||
this.response = response;
|
||||
}
|
||||
}
|
||||
|
||||
async function loadJson(url) {
|
||||
let response = await fetch(url);
|
||||
if (response.status == 200) {
|
||||
return response.json();
|
||||
} else {
|
||||
throw new HttpError(response);
|
||||
}
|
||||
}
|
||||
|
||||
// Ask for a user name until github returns a valid user
|
||||
async function demoGithubUser() {
|
||||
|
||||
let user;
|
||||
while(true) {
|
||||
let name = prompt("Enter a name?", "iliakan");
|
||||
|
||||
try {
|
||||
user = await loadJson(`https://api.github.com/users/${name}`);
|
||||
break; // no error, exit loop
|
||||
} catch(err) {
|
||||
if (err instanceof HttpError && err.response.status == 404) {
|
||||
// loop continues after the alert
|
||||
alert("No such user, please reenter.");
|
||||
} else {
|
||||
// unknown error, rethrow
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
alert(`Full name: ${user.name}.`);
|
||||
return user;
|
||||
}
|
||||
|
||||
demoGithubUser();
|
||||
```
|
48
1-js/11-async/07-async-await/02-rewrite-async-2/task.md
Normal file
48
1-js/11-async/07-async-await/02-rewrite-async-2/task.md
Normal file
|
@ -0,0 +1,48 @@
|
|||
|
||||
# Rewrite "rethrow" async/await
|
||||
|
||||
Below you can find the "rethrow" example from the chapter <info:promise-chaining>. Rewrite it using `async/await` instead of `.then/catch`.
|
||||
|
||||
And get rid of the recursion in favour of a loop in `demoGithubUser`: with `async/await` that becomes easy to do.
|
||||
|
||||
```js run
|
||||
class HttpError extends Error {
|
||||
constructor(response) {
|
||||
super(`${response.status} for ${response.url}`);
|
||||
this.name = 'HttpError';
|
||||
this.response = response;
|
||||
}
|
||||
}
|
||||
|
||||
function loadJson(url) {
|
||||
return fetch(url)
|
||||
.then(response => {
|
||||
if (response.status == 200) {
|
||||
return response.json();
|
||||
} else {
|
||||
throw new HttpError(response);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Ask for a user name until github returns a valid user
|
||||
function demoGithubUser() {
|
||||
let name = prompt("Enter a name?", "iliakan");
|
||||
|
||||
return loadJson(`https://api.github.com/users/${name}`)
|
||||
.then(user => {
|
||||
alert(`Full name: ${user.name}.`);
|
||||
return user;
|
||||
})
|
||||
.catch(err => {
|
||||
if (err instanceof HttpError && err.response.status == 404) {
|
||||
alert("No such user, please reenter.");
|
||||
return demoGithubUser();
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
demoGithubUser();
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue