You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/11-async/02-promise-basics/article.md
+12-12Lines changed: 12 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ Everyone is happy, because the people don't crowd you any more, and fans, becaus
8
8
9
9
This is a real-life analogy for things we often have in programming:
10
10
11
-
1. A "producing code" that does something and takes time. For instance, the code loads a remote script. That's a "singer".
11
+
1. A "producing code" that does something and takes time. For instance, the code loads data over a network. That's a "singer".
12
12
2. A "consuming code" that wants the result of the "producing code" once it's ready. Many functions may need that result. These are the "fans".
13
13
3. A *promise* is a special JavaScript object that links the "producing code" and the "consuming code" together. In terms of our analogy: this is the "subscription list". The "producing code" takes whatever time it needs to produce the promised result, and the "promise" makes that result available to all of the subscribed code when it's ready.
14
14
@@ -27,7 +27,7 @@ The function passed to `new Promise` is called the *executor*. When the promise
27
27
The resulting `promise` object has internal properties:
28
28
29
29
-`state` — initially "pending", then changes to either "fulfilled" or "rejected",
30
-
-`result` — an arbitrary value of your choosing, initially `undefined`.
30
+
-`result` — an arbitrary value, initially `undefined`.
31
31
32
32
When the executor finishes the job, it should call one of the functions that it gets as arguments:
33
33
@@ -56,7 +56,7 @@ let promise = new Promise(function(resolve, reject) {
56
56
We can see two things by running the code above:
57
57
58
58
1. The executor is called automatically and immediately (by the `new Promise`).
59
-
2. The executor receives two arguments: `resolve` and `reject` — these functions are pre-defined by the JavaScript engine. So we don't need to create them. Instead, we should write the executor to call them when ready.
59
+
2. The executor receives two arguments: `resolve` and `reject` — these functions are pre-defined by the JavaScript engine. So we don't need to create them. We only should call one of them when ready.
60
60
61
61
After one second of "processing" the executor calls `resolve("done")` to produce the result:
62
62
@@ -77,7 +77,7 @@ let promise = new Promise(function(resolve, reject) {
77
77
78
78
To summarize, the executor should do a job (something that takes time usually) and then call `resolve` or `reject` to change the state of the corresponding Promise object.
79
79
80
-
The Promise that is either resolved or rejected is called "settled", as opposed to a "pending" Promise.
80
+
The Promise that is either resolved or rejected is called "settled", as opposed to a initially "pending" Promise.
81
81
82
82
````smart header="There can be only a single result or an error"
83
83
The executor should call only one `resolve` or one `reject`. The promise's state change is final.
@@ -112,9 +112,9 @@ let promise = new Promise(function(resolve, reject) {
112
112
});
113
113
```
114
114
115
-
For instance, this might happen when we start to do a job but then see that everything has already been completed.
115
+
For instance, this might happen when we start to do a job but then see that everything has already been completed and cached.
116
116
117
-
That's fine. We immediately have a resolved Promise, nothing wrong with that.
117
+
That's fine. We immediately have a resolved promise.
118
118
````
119
119
120
120
```smart header="The `state` and `result` are internal"
@@ -140,12 +140,12 @@ promise.then(
140
140
141
141
The first argument of `.then` is a function that:
142
142
143
-
1. runs when the Promise is resolved, and
143
+
1. runs when the promise is resolved, and
144
144
2. receives the result.
145
145
146
146
The second argument of `.then` is a function that:
147
147
148
-
1. runs when the Promise is rejected, and
148
+
1. runs when the promise is rejected, and
149
149
2. receives the error.
150
150
151
151
For instance, here's a reaction to a successfully resolved promise:
@@ -233,10 +233,10 @@ new Promise((resolve, reject) => {
233
233
.then(result => show result, err => show error)
234
234
```
235
235
236
-
It's not exactly an alias though. There are several important differences:
236
+
It's not exactly an alias of `then(f,f)` though. There are several important differences:
237
237
238
238
1. A `finally` handler has no arguments. In `finally` we don't know whether the promise is successful or not. That's all right, as our task is usually to perform "general" finalizing procedures.
239
-
2. Finally passes through results and errors to the next handler.
239
+
2. A `finally` handler passes through results and errors to the next handler.
240
240
241
241
For instance, here the result is passed through `finally` to `then`:
242
242
```js run
@@ -257,11 +257,11 @@ It's not exactly an alias though. There are several important differences:
257
257
.catch(err => alert(err)); // <-- .catch handles the error object
258
258
```
259
259
260
-
That's very convenient, because finally is not meant to process promise results. So it passes them through.
260
+
That's very convenient, because `finally` is not meant to process a promise result. So it passes it through.
261
261
262
262
We'll talk more about promise chaining and result-passing between handlers in the next chapter.
263
263
264
-
3. Last, but not least, `.finally(f)` is a more convenient syntax than `.then(f, f)`: no need to duplicate the function.
264
+
3. Last, but not least, `.finally(f)` is a more convenient syntax than `.then(f, f)`: no need to duplicate the function `f`.
Copy file name to clipboardExpand all lines: 2-ui/4-forms-controls/2-focus-blur/5-keyboard-mouse/solution.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
2
-
We can use `mouse.onclick` to handle the click and make the mouse "moveable" with `position:fixed`, then then `mouse.onkeydown` to handle arrow keys.
2
+
We can use `mouse.onclick` to handle the click and make the mouse "moveable" with `position:fixed`, then `mouse.onkeydown` to handle arrow keys.
3
3
4
4
The only pitfall is that `keydown` only triggers on elements with focus. So we need to add `tabindex` to the element. As we're forbidden to change HTML, we can use `mouse.tabIndex` property for that.
Copy file name to clipboardExpand all lines: 2-ui/4-forms-controls/2-focus-blur/article.md
+2-3Lines changed: 2 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,13 +2,13 @@
2
2
3
3
An element receives a focus when the user either clicks on it or uses the `key:Tab` key on the keyboard. There's also an `autofocus` HTML attribute that puts the focus into an element by default when a page loads and other means of getting a focus.
4
4
5
-
Focusing generally means: "prepare to accept the data here", so that's the moment when we can run the code to initialize or load something.
5
+
Focusing on an element generally means: "prepare to accept the data here", so that's the moment when we can run the code to initialize the required functionality.
6
6
7
7
The moment of losing the focus ("blur") can be even more important. That's when a user clicks somewhere else or presses `key:Tab` to go to the next form field, or there are other means as well.
8
8
9
9
Losing the focus generally means: "the data has been entered", so we can run the code to check it or even to save it to the server and so on.
10
10
11
-
There are important peculiarities when working with focus events. We'll do the best to cover them here.
11
+
There are important peculiarities when working with focus events. We'll do the best to cover them further on.
12
12
13
13
## Events focus/blur
14
14
@@ -203,7 +203,6 @@ So here's another working variant:
203
203
204
204
<script>
205
205
*!*
206
-
// put the handler on capturing phase (last argument true)
Copy file name to clipboardExpand all lines: 2-ui/5-loading/02-script-async-defer/article.md
+9-11Lines changed: 9 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,11 +29,11 @@ There are some workarounds to that. For instance, we can put a script at the bot
29
29
</body>
30
30
```
31
31
32
-
But this solution is far from perfect. For example, the browser actually notices the script (and can start downloading it) only after it downloaded the full HTML document. For long HTML documents, that may be a noticeable delay.
32
+
But this solution is far from perfect. For example, the browser notices the script (and can start downloading it) only after it downloaded the full HTML document. For long HTML documents, that may be a noticeable delay.
33
33
34
-
Such things are invisible for people using very fast connections, but many people in the world still have slower internet speeds and far-from-perfect mobile connectivity.
34
+
Such things are invisible for people using very fast connections, but many people in the world still have slower internet speeds and use far-from-perfect mobile internet.
35
35
36
-
Luckily, there are two `<script>` attributes that solve the problem for us: `defer` and `async`
36
+
Luckily, there are two `<script>` attributes that solve the problem for us: `defer` and `async`.
37
37
38
38
## defer
39
39
@@ -86,7 +86,7 @@ But the specification requires scripts to execute in the document order, so it w
86
86
```
87
87
88
88
```smart header="The `defer` attribute is only for external scripts"
89
-
The `defer` attribute is ignored if the script has no `src`.
89
+
The `defer` attribute is ignored if the `<script>` tag has no `src`.
90
90
```
91
91
92
92
@@ -120,16 +120,17 @@ So, if we have several `async` scripts, they may execute in any order. Whatever
120
120
2.`DOMContentLoaded` may happen both before and after `async`, no guarantees here.
121
121
3. Async scripts don't wait for each other. A smaller script `small.js` goes second, but probably loads before `long.js`, so runs first. That's called a "load-first" order.
122
122
123
-
Async scripts are great when we integrate an independent third-party script into the page: counters, ads and so on.
123
+
Async scripts are great when we integrate an independent third-party script into the page: counters, ads and so on, as they don't depend on our scripts, and our scripts shouldn't wait for them:
124
124
125
125
```html
126
+
<!-- Google Analytics is usually added like this -->
We can also create a script dynamically using JavaScript:
133
+
We can also add a script dynamically using JavaScript:
133
134
134
135
```js run
135
136
let script =document.createElement('script');
@@ -145,7 +146,7 @@ That is:
145
146
- They don't wait for anything, nothing waits for them.
146
147
- The script that loads first -- runs first ("load-first" order).
147
148
148
-
We can change the load-first order into the document order by explicitly setting `async` to `false`:
149
+
We can change the load-first order into the document order (just like regular scripts) by explicitly setting `async` property to `false`:
149
150
150
151
```js run
151
152
let script =document.createElement('script');
@@ -191,7 +192,4 @@ Please note that if you're using `defer`, then the page is visible before the sc
191
192
192
193
So, buttons should be disabled by CSS or by other means, to let the user
193
194
194
-
In practice, `defer` is used for scripts that need DOM and/or their relative execution order is important.
195
-
196
-
197
-
So `async` is used for independent scripts, like counters or ads, that don't need to access page content. And their relative execution order does not matter.
195
+
In practice, `defer` is used for scripts that need the whole DOM and/or their relative execution order is important. And `async` is used for independent scripts, like counters or ads. And their relative execution order does not matter.
0 commit comments