Skip to content

Commit cce95cb

Browse files
committed
up
1 parent e8db0f9 commit cce95cb

File tree

13 files changed

+16955
-50
lines changed

13 files changed

+16955
-50
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
```js run
2+
function delay(ms) {
3+
return new Promise(resolve => setTimeout(resolve, ms));
4+
}
5+
6+
delay(3000).then(() => alert('runs after 3 seconds'));
7+
```
8+
9+
Please note that in this task `resolve` is called without arguments. We don't return any value from `delay`, just ensure the delay.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
# Delay with a promise
3+
4+
The built-in function `setTimeout` uses callbacks. Create a promise-based alternative.
5+
6+
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:
7+
8+
```js
9+
function delay(ms) {
10+
// your code
11+
}
12+
13+
delay(3000).then(() => alert('runs after 3 seconds'));
14+
```

8-async/02-promise-basics/03-animate-circle-promise/solution.md

Whitespace-only changes.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<style>
7+
.message-ball {
8+
font-size: 20px;
9+
line-height: 200px;
10+
text-align: center;
11+
}
12+
.circle {
13+
transition-property: width, height, margin-left, margin-top;
14+
transition-duration: 2s;
15+
position: fixed;
16+
transform: translateX(-50%) translateY(-50%);
17+
background-color: red;
18+
border-radius: 50%;
19+
}
20+
</style>
21+
</head>
22+
23+
<body>
24+
25+
<button onclick="go()">Click me</button>
26+
27+
<script>
28+
29+
function go() {
30+
showCircle(150, 150, 100).then(div => {
31+
div.classList.add('message-ball');
32+
div.append("Hello, world!");
33+
});
34+
}
35+
36+
function showCircle(cx, cy, radius) {
37+
let div = document.createElement('div');
38+
div.style.width = 0;
39+
div.style.height = 0;
40+
div.style.left = cx + 'px';
41+
div.style.top = cy + 'px';
42+
div.className = 'circle';
43+
document.body.append(div);
44+
45+
return new Promise(resolve => {
46+
setTimeout(() => {
47+
div.style.width = radius * 2 + 'px';
48+
div.style.height = radius * 2 + 'px';
49+
50+
div.addEventListener('transitionend', function handler() {
51+
div.removeEventListener('transitionend', handler);
52+
resolve(div);
53+
});
54+
}, 0);
55+
})
56+
}
57+
</script>
58+
59+
60+
</body>
61+
</html>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
# Animated circle with promise
3+
4+
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.
5+
6+
The new usage:
7+
8+
```js
9+
showCircle(150, 150, 100).then(div => {
10+
div.classList.add('message-ball');
11+
div.append("Hello, world!");
12+
});
13+
```
14+
15+
Take the solution of the task <info:task/animate-circle-callback> as the base.

0 commit comments

Comments
 (0)