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/01-callbacks/article.md
+17-7Lines changed: 17 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,19 @@
2
2
3
3
# Introduction: callbacks
4
4
5
+
```warn header="We use browser methods here"
6
+
To demonstrate the use of callbacks, promises and other abstract concepts, we'll use browser methods. Namely, load scripts and perform simple document manipulations.
7
+
8
+
If you're not familiar with them, please read few chapters from the [next part](/document) of the tutorial.
9
+
10
+
Or, maybe examples will be clear enough for you even without that.
11
+
```
12
+
5
13
Many actions in JavaScript are *asynchronous*.
6
14
7
-
For instance, take a look at the function `loadScript(src)`:
15
+
For instance, we can schedule such actions using `setTimeout`.
16
+
17
+
There are other real-world examples of asynchronous actions, e.g. take a look at the function `loadScript(src)`, that should load a new script with the given `src`:
8
18
9
19
```js
10
20
functionloadScript(src) {
@@ -14,18 +24,18 @@ function loadScript(src) {
14
24
}
15
25
```
16
26
17
-
The purpose of the function is to load a new script. When it adds the`<script src="…">` to the document, the browser loads and executes it.
27
+
When it appends the new, dynamically created, tag`<script src="…">` to the document, the browser loads and executes it.
18
28
19
-
We can use it like this:
29
+
We can use this function like this:
20
30
21
31
```js
22
-
//loads and executes the script
32
+
//load and execute the script at the given path
23
33
loadScript('/my/script.js');
24
34
```
25
35
26
-
The function is called "asynchronously," because the action (script loading) finishes not now, but later.
36
+
The script is executed "asynchronously", because script loading finishes not right now, but later.
27
37
28
-
If there's a code below `loadScript(…)`, it doesn't wait until the loading finishes.
38
+
If there's a code below `loadScript(…)`, it doesn't wait until the script loading finishes.
29
39
30
40
```js
31
41
loadScript('/my/script.js');
@@ -34,7 +44,7 @@ loadScript('/my/script.js');
34
44
// ...
35
45
```
36
46
37
-
We'd like to use the new script as soon as it loads. It declares new functions, and we want to run them.
47
+
Let's say we need to use the new script as soon as it loads. It declares new functions, and we want to run them.
38
48
39
49
But if we do that immediately after the `loadScript(…)` call, that wouldn't work:
0 commit comments