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: 6-async/01-callbacks/article.md
+8-8
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
# Introduction: callbacks
4
4
5
-
Many actions in Javascript are *asynchronous*.
5
+
Many actions in JavaScript are *asynchronous*.
6
6
7
7
For instance, take a look at the function `loadScript(src)`:
8
8
@@ -23,7 +23,7 @@ We can use it like this:
23
23
loadScript('/my/script.js');
24
24
```
25
25
26
-
The function is called "asynchronous", because the action (script loading) finishes not now, but later.
26
+
The function is called "asynchronously", because the action (script loading) finishes not now, but later.
27
27
28
28
The call initiates the script loading, then the execution continues. While the script is loading, the code below may finish executing, and if the loading takes time, other scripts may run meanwhile too.
29
29
@@ -35,7 +35,7 @@ loadScript('/my/script.js');
35
35
36
36
Now let's say we want to use the new script when it loads. It probably declares new functions, so we'd like to run them.
37
37
38
-
...But if we do that immediately after the `loadScript(…)` call, that wouldn't work:
38
+
But if we do that immediately after the `loadScript(…)` call, that wouldn't work:
39
39
40
40
```js
41
41
loadScript('/my/script.js'); // the script has "function newFunction() {…}"
@@ -45,7 +45,7 @@ newFunction(); // no such function!
45
45
*/!*
46
46
```
47
47
48
-
Naturally, the browser probably didn't have time to load the script. So the immediate call to the new function fails. As of now, `loadScript` function 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 new functions and variables from that script.
48
+
Naturally, the browser probably didn't have time to load the script. So the immediate call to the new function fails. As of now, `loadScript` function 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 it happens, to use new functions and variables from that script.
49
49
50
50
Let's add a `callback` function as a second argument to `loadScript` that should execute when the script loads:
After the outer `loadScript` is complete, the callback initiates the inner one.
120
120
121
-
...What if we want one more script?
121
+
What if we want one more script...?
122
122
123
123
```js
124
124
loadScript('/my/script.js', function(script) {
@@ -183,7 +183,7 @@ So the single `callback` function is used both for reporting errors and passing
183
183
184
184
From the first look it's a viable way of asynchronous coding. And indeed it is. For one or maybe two nested calls it looks fine.
185
185
186
-
But for multiple asynchronous actions that follow one after another we'll have a code like this:
186
+
But for multiple asynchronous actions that follow one after another we'll have code like this:
187
187
188
188
```js
189
189
loadScript('1.js', function(error, script) {
@@ -264,8 +264,8 @@ See? It does the same, and there's no deep nesting now, because we made every ac
264
264
265
265
It works, but the code looks like a torn apart spreadsheet. It's difficult to read, you probably noticed that. One needs to eye-jump between pieces while reading it. That's inconvenient, especially the reader is not familiar with the code and doesn't know where to eye-jump.
266
266
267
-
Also the functions named `step*` are all of a single use, they are created only to evade the "pyramid of doom". No one is going to reuse them outside of the action chain. So there's a bit of a namespace cluttering here.
267
+
Also the functions named `step*` are all of a single use, they are created only to avoid the "pyramid of doom". No one is going to reuse them outside of the action chain. So there's a bit of a namespace cluttering here.
268
268
269
269
We'd like to have a something better.
270
270
271
-
Luckily, there are other ways to evade such pyramids. One of the best ways is to use "promises", described in the next chapter.
271
+
Luckily, there are other ways to avoid such pyramids. One of the best ways is to use "promises", described in the next chapter.
0 commit comments