Skip to content

Promise #193

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 1-js/11-async/02-promise-basics/01-re-resolve/solution.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
The output is: `1`.
Výstup je: `1`.

The second call to `resolve` is ignored, because only the first call of `reject/resolve` is taken into account. Further calls are ignored.
Druhé volání `splň` se ignoruje, protože v úvahu se bere vždy jen první volání `splň/zamítni`. Další volání jsou ignorována.
12 changes: 6 additions & 6 deletions 1-js/11-async/02-promise-basics/01-re-resolve/task.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

# Re-resolve a promise?
# Znovusplnění příslibu?


What's the output of the code below?
Jaký je výstup následujícího kódu?

```js
let promise = new Promise(function(resolve, reject) {
resolve(1);
let příslib = new Promise(function(splň, zamítni) {
splň(1);

setTimeout(() => resolve(2), 1000);
setTimeout(() => splň(2), 1000);
});

promise.then(alert);
příslib.then(alert);
```
8 changes: 4 additions & 4 deletions 1-js/11-async/02-promise-basics/02-delay-promise/solution.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
```js run
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
function čekej(ms) {
return new Promise(splň => setTimeout(splň, ms));
}

delay(3000).then(() => alert('runs after 3 seconds'));
čekej(3000).then(() => alert('spustí se za 3 sekundy'));
```

Please note that in this task `resolve` is called without arguments. We don't return any value from `delay`, just ensure the delay.
Prosíme všimněte si, že v této úloze je `splň` voláno bez argumentů. Z funkce `čekej` nevracíme žádnou hodnotu, jen zajistíme čekání.
12 changes: 6 additions & 6 deletions 1-js/11-async/02-promise-basics/02-delay-promise/task.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@

# Delay with a promise
# Čekání s příslibem

The built-in function `setTimeout` uses callbacks. Create a promise-based alternative.
Vestavěná funkce `setTimeout` používá callbacky. Vytvořte alternativu založenou na příslibech.

The function `delay(ms)` should return a promise. That promise should resolve after `ms` milliseconds, so that we can add `.then` to it, like this:
Funkce `čekej(ms)` by měla vrátit příslib. Tento příslib by se měl splnit za `ms` milisekund, takže do něj můžeme přidat `.then`, například:

```js
function delay(ms) {
// your code
function čekej(ms) {
// váš kód
}

delay(3000).then(() => alert('runs after 3 seconds'));
čekej(3000).then(() => alert('spustí se za 3 sekundy'));
```
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@

<body>

<button onclick="go()">Click me</button>
<button onclick="spusť()">Klikni na mne</button>

<script>

function go() {
showCircle(150, 150, 100).then(div => {
function spusť() {
zobrazKruh(150, 150, 100).then(div => {
div.classList.add('message-ball');
div.append("Hello, world!");
div.append("Ahoj, světe!");
});
}

function showCircle(cx, cy, radius) {
function zobrazKruh(cx, cy, poloměr) {
let div = document.createElement('div');
div.style.width = 0;
div.style.height = 0;
Expand All @@ -44,8 +44,8 @@

return new Promise(resolve => {
setTimeout(() => {
div.style.width = radius * 2 + 'px';
div.style.height = radius * 2 + 'px';
div.style.width = poloměr * 2 + 'px';
div.style.height = poloměr * 2 + 'px';

div.addEventListener('transitionend', function handler() {
div.removeEventListener('transitionend', handler);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

# Animated circle with promise
# Animovaný kruh s příslibem

Rewrite the `showCircle` function in the solution of the task <info:task/animate-circle-callback> so that it returns a promise instead of accepting a callback.
Přepište funkci `zobrazKruh` v řešení úlohy <info:task/animate-circle-callback> tak, aby místo přijímání callbacku vracela příslib.

The new usage:
Nové použití:

```js
showCircle(150, 150, 100).then(div => {
zobrazKruh(150, 150, 100).then(div => {
div.classList.add('message-ball');
div.append("Hello, world!");
div.append("Ahoj, světe!");
});
```

Take the solution of the task <info:task/animate-circle-callback> as the base.
Jako základ vezměte řešení úlohy <info:task/animate-circle-callback>.
357 changes: 179 additions & 178 deletions 1-js/11-async/02-promise-basics/article.md

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions 1-js/11-async/02-promise-basics/head.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<script>
function loadScript(src) {
function načtiSkript(src) {
return new Promise(function(resolve, reject) {
let script = document.createElement('script');
script.src = src;
let skript = document.createElement('script');
skript.src = src;

script.onload = () => resolve(script);
script.onerror = () => reject(new Error("Script load error: " + src));
skript.onload = () => resolve(skript);
skript.onerror = () => reject(new Error("Chyba při načítání skriptu: " + src));

document.head.append(script);
document.head.append(skript);
});
}
</script>
2 changes: 1 addition & 1 deletion 1-js/11-async/02-promise-basics/promise-reject-1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion 1-js/11-async/02-promise-basics/promise-resolve-1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion 1-js/11-async/02-promise-basics/promise-resolve-reject.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.