Skip to content

Commit 04b2fcf

Browse files
committed
promises
1 parent a68022a commit 04b2fcf

File tree

17 files changed

+304
-137
lines changed

17 files changed

+304
-137
lines changed

1-js/07-object-oriented-programming/03-prototype-inheritance/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ let user = {
174174

175175
set fullName(value) {
176176
[this.name, this.surname] = value.split(" ");
177-
}
177+
},
178178

179179
get fullName() {
180180
return `${this.name} ${this.surname}`;

2-ui/1-document/06-dom-attributes-and-properties/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ Please note:
139139

140140
1. `getAttribute('About')` -- the first letter is uppercase here, and in HTML it's all lowercase. But that doesn't matter: attribute names are case-insensitive.
141141
2. We can assign anything to an attribute, but that becomes a string. So here we have `"123"` as the value.
142-
3. All attributes including ones that we set are seen in `innerHTML`.
142+
3. All attributes including ones that we set are visible in `outerHTML`.
143143
4. The `attributes` collection is iterable and has all attributes with `name` and `value`.
144144

145145
## Property-attribute synchronization

2-ui/3-event-details/10-onload-ondomcontentloaded/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ There are two differences between them.
6767
| | `async` | `defer` |
6868
|---------|---------|---------|
6969
| Order | Scripts with `async` execute *in the load-first order*. Their document order doesn't matter -- which loads first runs first. | Scripts with `defer` always execute *in the document order* (as they go in the document). |
70-
| `DOMContentLoaded` | Scripts with `async` may load and execute while the document has not yet been fully downloaded. | Scripts with `defer` execute after the document is loaded and parsed (they wait if needed), right before `DOMContentLoaded`. |
70+
| `DOMContentLoaded` | Scripts with `async` may load and execute while the document has not yet been fully downloaded. That happens if scripts are small or cached, and the document is long enough. | Scripts with `defer` execute after the document is loaded and parsed (they wait if needed), right before `DOMContentLoaded`. |
7171

7272
So `async` is used for totally independent scripts.
7373

5-animation/3-js-animation/article.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ Function `animate` accepts 3 parameters that essentially describes the animation
186186

187187
Let's animate the element `width` from `0` to `100%` using our function.
188188

189-
Click for the demo:
189+
Click on the element for the demo:
190190

191191
[codetabs height=60 src="width"]
192192

@@ -410,19 +410,19 @@ As we can see, the graph of the first half of the animation is the scaled down `
410410

411411
## More interesting "draw"
412412

413-
Instead of moving the element we can do something else. All we need is to write the write `draw`
414-
415-
### Typing in the text
413+
Instead of moving the element we can do something else. All we need is to write the write the proper `draw`.
416414

417415
Here's the animated "bouncing" text typing:
418416

419417
[codetabs src="text"]
420418

421419
## Summary
422420

423-
JavaScript animation is implemented with the help of `requestAnimationFrame`.
421+
JavaScript animation should be implemented via `requestAnimationFrame`. That built-in method allows to setup a callback function to run when the browser will be preparing a repaint. Usually that's very soon, but the exact time depends on the browser.
422+
423+
When a page is in the background, there are no repaints at all, so the callback won't run: the animation will be suspended and won't consume resources. That's great.
424424

425-
The helper `animate` function:
425+
Here's the helper `animate` function to setup most animations:
426426

427427
```js
428428
function animate({timing, draw, duration}) {

5-animation/3-js-animation/text.view/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<body>
1111

1212

13-
<textarea id="textExample" rows="4" cols="60">He took his vorpal sword in hand:
13+
<textarea id="textExample" rows="5" cols="60">He took his vorpal sword in hand:
1414
Long time the manxome foe he sought—
1515
So rested he by the Tumtum tree,
1616
And stood awhile in thought.

8-async/01-callback-hell/article.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,35 @@ A *synchronous* action suspends the execution until it's completed. For instance
3030
```js
3131
let age = prompt("How old are you", 20);
3232
// the execution of the code below awaits for the prompt to finish
33+
// the script hangs
3334
```
3435

3536
An *asynchronous* action allows the program to continue while it's in progress. For instance, a call to `loadScript` is asynchronous. It initiates the script loading, but does not suspend the execution. Other commands may execute while the script is loading:
3637

3738
```js
3839
loadScript('/my/script.js');
39-
// the execution does not wait for the script loading to finish,
40-
// it just goes on
40+
// the execution of the code below *does not* wait for the script loading to finish,
41+
// it just continues
4142
```
4243

43-
As of now, `loadScript` provides no way to track the load completion. The script loads and eventually runs.
44+
As of now, the `loadScript` function loads the script, doesn't provide a way to track the load completion. The script loads and eventually runs, that's all. But we'd like to know when happens, to use additional functions and variables from that script.
4445

45-
Let's add a `callback` function as a second argument to `loadScript`, that should execute at when the script is loaded.
46+
Let's add a `callback` function as a second argument to `loadScript` that should execute when the script loads:
4647

4748
```js
4849
function loadScript(src, callback) {
4950
let script = document.createElement('script');
5051
script.src = src;
5152

53+
*!*
5254
script.onload = () => callback(script);
55+
*/!*
5356

5457
document.head.append(script);
5558
}
5659
```
5760

58-
Now we're able to load a script and run our code that can use new functions from it, like here:
61+
Now we're able to load a script and then run our code that can use new functions from it, like here:
5962

6063
```js run
6164
function loadScript(src, callback) {
@@ -73,7 +76,9 @@ loadScript('https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.2.0/lodash.js', f
7376
*/!*
7477
```
7578

76-
That's called a "callback style" of asynchronous programming. A function that does something asynchronously provides a callback argument where we put the code to run after it's complete.
79+
That's called a "callback-based" style of asynchronous programming. A function that does something asynchronously should provide a `callback` argument where we put the function to run after it's complete.
80+
81+
Here it was used for `loadScript`, but of course it's a general approach.
7782

7883
## Callback in callback
7984

@@ -151,10 +156,10 @@ loadScript('/my/script.js', function(error, script) {
151156
```
152157

153158
The convention is:
154-
1. The first argument of `callback` is reserved for an error if it occurs.
155-
2. The second argument (and successive ones if needed) are for the successful result.
159+
1. The first argument of `callback` is reserved for an error if it occurs. Then `callback(err)` is called.
160+
2. The second argument and successive ones if needed are for the successful result. Then `callback(null, result1, result2…)` is called.
156161

157-
So the single `callback` function is used both for reporting errors and passing back results.
162+
So the single `callback` function is used both for reporting errors and passing back results. That's called "error-first callback" style. Or we could use different functions for successful and erroneous completion.
158163

159164
## Pyramid of doom
160165

@@ -203,6 +208,8 @@ That's sometimes called "callback hell" or "pyramid of doom".
203208

204209
The pyramid grows to the right with every asynchronous action. Soon it spirales out of control.
205210

211+
So this way of coding appears not so good.
212+
206213
In simple cases we can evade the problem by making every action a standalone function, like this:
207214

208215
```js
@@ -235,6 +242,10 @@ function step3(error, script) {
235242
};
236243
```
237244

238-
See? There's no deep nesting now, because we moved every function to the top. But the code looks like a torn apart spreadsheet. We need to eye-jump between pieces while reading it. The functions `step*` have no use, they are only created to evade the "pyramid of doom".
245+
See? It does the same, and there's no deep nesting now, because we moved every function to the top. But the code looks like a torn apart spreadsheet. We need to eye-jump between pieces while reading it. It's not very readable, especially if you are not familiar with it and don't know where to eye-jump.
246+
247+
Also the functions `step*` have no use, they are only created to evade the "pyramid of doom".
248+
249+
So we'd like to have a better way of coding for complex asynchronous actions.
239250

240-
Luckily, there are other ways to evade such pyramids. Most modern code makes use of "promises", we'll study them in the next chapter.
251+
Luckily, there are other ways to evade such pyramids. For instance, we can use "promises", described in the next chapter.

0 commit comments

Comments
 (0)