Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3
Async/await#199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Async/await#199
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
32 changes: 16 additions & 16 deletions1-js/11-async/08-async-await/01-rewrite-async/solution.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,33 @@ | ||
Poznámky jsou pod kódem: | ||
```js run | ||
async functionnačtiJson(url) { // (1) | ||
letodpověď = await fetch(url); // (2) | ||
if (odpověď.status == 200) { | ||
let json = awaitodpověď.json(); // (3) | ||
return json; | ||
} | ||
throw new Error(odpověď.status); | ||
} | ||
načtiJson('https://javascript.info/takovy-uzivatel-neni.json') | ||
.catch(alert); //Chyba: 404 (4) | ||
``` | ||
Poznámky: | ||
1.Funkce `načtiJson` se stává asynchronní (`async`). | ||
2.Všechna `.then`uvnitř jsou nahrazena za `await`. | ||
3.Můžeme vrátit `returnodpověď.json()`místo čekání na tuto funkci, například: | ||
```js | ||
if (odpověď.status == 200) { | ||
returnodpověď.json(); // (3) | ||
} | ||
``` | ||
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ží. | ||
4.Chyba vyvolaná z `načtiJson` je ošetřena pomocí `.catch`.Nemůžeme zde použít `awaitnačtiJson(…)`, protože nejsme uvnitř funkce s `async`. |
18 changes: 9 additions & 9 deletions1-js/11-async/08-async-await/01-rewrite-async/task.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
#Přepište za použití async/await | ||
Přepište tento příklad kódu z kapitoly<info:promise-chaining>za použití`async/await`namísto `.then/catch`: | ||
```js run | ||
functionnačtiJson(url) { | ||
return fetch(url) | ||
.then(odpověď => { | ||
if (odpověď.status == 200) { | ||
returnodpověď.json(); | ||
} else { | ||
throw new Error(odpověď.status); | ||
} | ||
}); | ||
} | ||
načtiJson('https://javascript.info/takovy-uzivatel-neni.json') | ||
.catch(alert); //Chyba: 404 | ||
``` |
52 changes: 26 additions & 26 deletions1-js/11-async/08-async-await/02-rewrite-async-2/solution.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,49 @@ | ||
Nejsou tady žádné triky. Stačí uvnitř `demoUživatelGitHubu` nahradit `.catch`za `try..catch`a přidat`async/await`, kde jsou zapotřebí: | ||
```js run | ||
classChybaHttp extends Error { | ||
constructor(odpověď) { | ||
super(`${odpověď.status}pro ${odpověď.url}`); | ||
this.name = 'ChybaHttp'; | ||
this.odpověď =odpověď; | ||
} | ||
} | ||
async functionnačtiJson(url) { | ||
letodpověď = await fetch(url); | ||
if (odpověď.status == 200) { | ||
returnodpověď.json(); | ||
} else { | ||
throw newChybaHttp(odpověď); | ||
} | ||
} | ||
//Ptáme se na uživatelské jméno, dokud GitHub nevrátí platného uživatele | ||
async functiondemoUživatelGitHubu() { | ||
letuživatel; | ||
while(true) { | ||
letjméno = prompt("Zadejte jméno", "iliakan"); | ||
try { | ||
uživatel = awaitnačtiJson(`https://api.github.com/users/${jméno}`); | ||
break; //žádná chyba, opustíme cyklus | ||
} catch(chyba) { | ||
if (chyba instanceofChybaHttp &&chyba.odpověď.status == 404) { | ||
//po alertu bude cyklus pokračovat | ||
alert("Takový uživatel neexistuje, prosím zadejte znovu."); | ||
} else { | ||
//neznámá chyba, vyvoláme ji znovu | ||
throwchyba; | ||
} | ||
} | ||
} | ||
alert(`Celé jméno: ${uživatel.name}.`); | ||
returnuživatel; | ||
} | ||
demoUživatelGitHubu(); | ||
``` |
52 changes: 26 additions & 26 deletions1-js/11-async/08-async-await/02-rewrite-async-2/task.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,48 @@ | ||
#Přepište „opětovné vyvolání“ za použití async/await | ||
Následuje příklad „opětovného vyvolání“. Přepište jej za použití`async/await`místo `.then/catch`. | ||
A ve funkci `demoUživatelGitHubu` se zbavte rekurze ve prospěch cyklu: s `async/await` tobude lehké. | ||
```js run | ||
classChybaHttp extends Error { | ||
constructor(odpověď) { | ||
super(`${odpověď.status}pro ${odpověď.url}`); | ||
this.name = 'ChybaHttp'; | ||
this.odpověď =odpověď; | ||
} | ||
} | ||
functionnačtiJson(url) { | ||
return fetch(url) | ||
.then(odpověď => { | ||
if (odpověď.status == 200) { | ||
returnodpověď.json(); | ||
} else { | ||
throw newChybaHttp(odpověď); | ||
} | ||
}); | ||
} | ||
//Ptáme se na uživatelské jméno, dokud GitHub nevrátí platného uživatele | ||
functiondemoUživatelGitHubu() { | ||
letjméno = prompt("Zadejte jméno", "iliakan"); | ||
returnnačtiJson(`https://api.github.com/users/${jméno}`) | ||
.then(uživatel => { | ||
alert(`Celé jméno: ${uživatel.name}.`); | ||
returnuživatel; | ||
}) | ||
.catch(chyba => { | ||
if (chyba instanceofChybaHttp &&chyba.odpověď.status == 404) { | ||
alert("Takový uživatel neexistuje, prosím zadejte znovu."); | ||
returndemoUživatelGitHubu(); | ||
} else { | ||
throwchyba; | ||
} | ||
}); | ||
} | ||
demoUživatelGitHubu(); | ||
``` |
10 changes: 5 additions & 5 deletions1-js/11-async/08-async-await/03-async-from-regular/solution.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
14 changes: 7 additions & 7 deletions1-js/11-async/08-async-await/03-async-from-regular/task.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
#Volání asynchronní funkce z neasynchronní | ||
Máme „obyčejnou“ funkci nazvanou`f`.Jak můžeme volat`async`funkci `čekej()`a použít její výsledek uvnitř `f`? | ||
```js | ||
async functiončekej() { | ||
await new Promise(resolve => setTimeout(resolve, 1000)); | ||
return 10; | ||
} | ||
function f() { | ||
// ...co byste sem měli napsat? | ||
//musíme volat asynchronní čekej()a čekat, než obdržíme 10 | ||
//pamatujte, že nemůžeme použít „await“ | ||
} | ||
``` | ||
P.S.Tento úkol je technicky velmi jednoduchý, ale tato otázka je u vývojářů, kteří sasync/await teprve začínají, vcelku běžná. |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.