Skip to content

Commit 9fbbf18

Browse files
authored
Merge pull request #199 from otmon76/1.11.8
Async/await
2 parents 09013a9 + 4109798 commit 9fbbf18

File tree

7 files changed

+204
-204
lines changed

7 files changed

+204
-204
lines changed
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11

2-
The notes are below the code:
2+
Poznámky jsou pod kódem:
33

44
```js run
5-
async function loadJson(url) { // (1)
6-
let response = await fetch(url); // (2)
5+
async function načtiJson(url) { // (1)
6+
let odpověď = await fetch(url); // (2)
77

8-
if (response.status == 200) {
9-
let json = await response.json(); // (3)
8+
if (odpověď.status == 200) {
9+
let json = await odpověď.json(); // (3)
1010
return json;
1111
}
1212

13-
throw new Error(response.status);
13+
throw new Error(odpověď.status);
1414
}
1515

16-
loadJson('https://javascript.info/no-such-user.json')
17-
.catch(alert); // Error: 404 (4)
16+
načtiJson('https://javascript.info/takovy-uzivatel-neni.json')
17+
.catch(alert); // Chyba: 404 (4)
1818
```
1919

20-
Notes:
20+
Poznámky:
2121

22-
1. The function `loadJson` becomes `async`.
23-
2. All `.then` inside are replaced with `await`.
24-
3. We can `return response.json()` instead of awaiting for it, like this:
22+
1. Funkce `načtiJson` se stává asynchronní (`async`).
23+
2. Všechna `.then` uvnitř jsou nahrazena za `await`.
24+
3. Můžeme vrátit `return odpověď.json()` místo čekání na tuto funkci, například:
2525

2626
```js
27-
if (response.status == 200) {
28-
return response.json(); // (3)
27+
if (odpověď.status == 200) {
28+
return odpověď.json(); // (3)
2929
}
3030
```
3131

32-
Then the outer code would have to `await` for that promise to resolve. In our case it doesn't matter.
33-
4. The error thrown from `loadJson` is handled by `.catch`. We can't use `await loadJson(…)` there, because we're not in an `async` function.
32+
Pak by vnější kód musel počkat pomocí `await`, než se tento příslib vyhodnotí. V našem případě na tom nezáleží.
33+
4. Chyba vyvolaná z `načtiJson` je ošetřena pomocí `.catch`. Nemůžeme zde použít `await načtiJson(…)`, protože nejsme uvnitř funkce s `async`.
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11

2-
# Rewrite using async/await
2+
# Přepište za použití async/await
33

4-
Rewrite this example code from the chapter <info:promise-chaining> using `async/await` instead of `.then/catch`:
4+
Přepište tento příklad kódu z kapitoly <info:promise-chaining> za použití `async/await` namísto `.then/catch`:
55

66
```js run
7-
function loadJson(url) {
7+
function načtiJson(url) {
88
return fetch(url)
9-
.then(response => {
10-
if (response.status == 200) {
11-
return response.json();
9+
.then(odpověď => {
10+
if (odpověď.status == 200) {
11+
return odpověď.json();
1212
} else {
13-
throw new Error(response.status);
13+
throw new Error(odpověď.status);
1414
}
1515
});
1616
}
1717

18-
loadJson('https://javascript.info/no-such-user.json')
19-
.catch(alert); // Error: 404
18+
načtiJson('https://javascript.info/takovy-uzivatel-neni.json')
19+
.catch(alert); // Chyba: 404
2020
```
Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
11

2-
There are no tricks here. Just replace `.catch` with `try..catch` inside `demoGithubUser` and add `async/await` where needed:
2+
Nejsou tady žádné triky. Stačí uvnitř `demoUživatelGitHubu` nahradit `.catch` za `try..catch` a přidat `async/await`, kde jsou zapotřebí:
33

44
```js run
5-
class HttpError extends Error {
6-
constructor(response) {
7-
super(`${response.status} for ${response.url}`);
8-
this.name = 'HttpError';
9-
this.response = response;
5+
class ChybaHttp extends Error {
6+
constructor(odpověď) {
7+
super(`${odpověď.status} pro ${odpověď.url}`);
8+
this.name = 'ChybaHttp';
9+
this.odpověď = odpověď;
1010
}
1111
}
1212

13-
async function loadJson(url) {
14-
let response = await fetch(url);
15-
if (response.status == 200) {
16-
return response.json();
13+
async function načtiJson(url) {
14+
let odpověď = await fetch(url);
15+
if (odpověď.status == 200) {
16+
return odpověď.json();
1717
} else {
18-
throw new HttpError(response);
18+
throw new ChybaHttp(odpověď);
1919
}
2020
}
2121

22-
// Ask for a user name until github returns a valid user
23-
async function demoGithubUser() {
22+
// Ptáme se na uživatelské jméno, dokud GitHub nevrátí platného uživatele
23+
async function demoUživatelGitHubu() {
2424

25-
let user;
25+
let uživatel;
2626
while(true) {
27-
let name = prompt("Enter a name?", "iliakan");
27+
let jméno = prompt("Zadejte jméno", "iliakan");
2828

2929
try {
30-
user = await loadJson(`https://api.github.com/users/${name}`);
31-
break; // no error, exit loop
32-
} catch(err) {
33-
if (err instanceof HttpError && err.response.status == 404) {
34-
// loop continues after the alert
35-
alert("No such user, please reenter.");
30+
uživatel = await načtiJson(`https://api.github.com/users/${jméno}`);
31+
break; // žádná chyba, opustíme cyklus
32+
} catch(chyba) {
33+
if (chyba instanceof ChybaHttp && chyba.odpověď.status == 404) {
34+
// po alertu bude cyklus pokračovat
35+
alert("Takový uživatel neexistuje, prosím zadejte znovu.");
3636
} else {
37-
// unknown error, rethrow
38-
throw err;
37+
// neznámá chyba, vyvoláme ji znovu
38+
throw chyba;
3939
}
4040
}
4141
}
4242

4343

44-
alert(`Full name: ${user.name}.`);
45-
return user;
44+
alert(`Celé jméno: ${ivatel.name}.`);
45+
return uživatel;
4646
}
4747

48-
demoGithubUser();
48+
demoUživatelGitHubu();
4949
```
Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
11

2-
# Rewrite "rethrow" with async/await
2+
# Přepište „opětovné vyvolání“ za použití async/await
33

4-
Below you can find the "rethrow" example. Rewrite it using `async/await` instead of `.then/catch`.
4+
Následuje příklad „opětovného vyvolání“. Přepište jej za použití `async/await` místo `.then/catch`.
55

6-
And get rid of the recursion in favour of a loop in `demoGithubUser`: with `async/await` that becomes easy to do.
6+
A ve funkci `demoUživatelGitHubu` se zbavte rekurze ve prospěch cyklu: s `async/await` to bude lehké.
77

88
```js run
9-
class HttpError extends Error {
10-
constructor(response) {
11-
super(`${response.status} for ${response.url}`);
12-
this.name = 'HttpError';
13-
this.response = response;
9+
class ChybaHttp extends Error {
10+
constructor(odpověď) {
11+
super(`${odpověď.status} pro ${odpověď.url}`);
12+
this.name = 'ChybaHttp';
13+
this.odpověď = odpověď;
1414
}
1515
}
1616

17-
function loadJson(url) {
17+
function načtiJson(url) {
1818
return fetch(url)
19-
.then(response => {
20-
if (response.status == 200) {
21-
return response.json();
19+
.then(odpověď => {
20+
if (odpověď.status == 200) {
21+
return odpověď.json();
2222
} else {
23-
throw new HttpError(response);
23+
throw new ChybaHttp(odpověď);
2424
}
2525
});
2626
}
2727

28-
// Ask for a user name until github returns a valid user
29-
function demoGithubUser() {
30-
let name = prompt("Enter a name?", "iliakan");
28+
// Ptáme se na uživatelské jméno, dokud GitHub nevrátí platného uživatele
29+
function demoUživatelGitHubu() {
30+
let jméno = prompt("Zadejte jméno", "iliakan");
3131

32-
return loadJson(`https://api.github.com/users/${name}`)
33-
.then(user => {
34-
alert(`Full name: ${user.name}.`);
35-
return user;
32+
return načtiJson(`https://api.github.com/users/${jméno}`)
33+
.then(ivatel => {
34+
alert(`Celé jméno: ${ivatel.name}.`);
35+
return uživatel;
3636
})
37-
.catch(err => {
38-
if (err instanceof HttpError && err.response.status == 404) {
39-
alert("No such user, please reenter.");
40-
return demoGithubUser();
37+
.catch(chyba => {
38+
if (chyba instanceof ChybaHttp && chyba.odpověď.status == 404) {
39+
alert("Takový uživatel neexistuje, prosím zadejte znovu.");
40+
return demoUživatelGitHubu();
4141
} else {
42-
throw err;
42+
throw chyba;
4343
}
4444
});
4545
}
4646

47-
demoGithubUser();
47+
demoUživatelGitHubu();
4848
```

1-js/11-async/08-async-await/03-async-from-regular/solution.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11

2-
That's the case when knowing how it works inside is helpful.
2+
Toto je případ, kdy nám pomáhá, že víme, jak to funguje uvnitř.
33

4-
Just treat `async` call as promise and attach `.then` to it:
4+
Jednoduše zacházejte s voláním `async` jako s příslibem a připojte k němu `.then`:
55
```js run
6-
async function wait() {
6+
async function čekej() {
77
await new Promise(resolve => setTimeout(resolve, 1000));
88

99
return 10;
1010
}
1111

1212
function f() {
13-
// shows 10 after 1 second
13+
// za 1 sekundu zobrazí 10
1414
*!*
15-
wait().then(result => alert(result));
15+
čekej().then(sledek => alert(výsledek));
1616
*/!*
1717
}
1818

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11

2-
# Call async from non-async
2+
# Volání asynchronní funkce z neasynchronní
33

4-
We have a "regular" function called `f`. How can you call the `async` function `wait()` and use its result inside of `f`?
4+
Máme „obyčejnou“ funkci nazvanou `f`. Jak můžeme volat `async` funkci `čekej()` a použít její výsledek uvnitř `f`?
55

66
```js
7-
async function wait() {
7+
async function čekej() {
88
await new Promise(resolve => setTimeout(resolve, 1000));
99

1010
return 10;
1111
}
1212

1313
function f() {
14-
// ...what should you write here?
15-
// we need to call async wait() and wait to get 10
16-
// remember, we can't use "await"
14+
// ...co byste sem měli napsat?
15+
// musíme volat asynchronní čekej() a čekat, než obdržíme 10
16+
// pamatujte, že nemůžeme použít „await
1717
}
1818
```
1919

20-
P.S. The task is technically very simple, but the question is quite common for developers new to async/await.
20+
P.S. Tento úkol je technicky velmi jednoduchý, ale tato otázka je u vývojářů, kteří s async/await teprve začínají, vcelku běžná.

0 commit comments

Comments
 (0)