Skip to content

Commit bcb1fb6

Browse files
authored
Merge pull request #276 from odsantos/update--en-introduction-callbacks
Update "Introduction callbacks" English article
2 parents 15d754c + 5e65f9b commit bcb1fb6

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

1-js/11-async/01-callbacks/article.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ If there's any code below `loadScript(…)`, it doesn't wait until the script lo
4343

4444
```js
4545
loadScript('/my/script.js');
46-
// the code below loadScript doesn't wait for the script loading to finish
46+
// the code below loadScript
47+
// doesn't wait for the script loading to finish
4748
// ...
4849
```
4950

@@ -59,7 +60,7 @@ newFunction(); // no such function!
5960
*/!*
6061
```
6162

62-
Naturally, the browser probably didn't have time to load the script. So the immediate call to the new function fails. As of now, the `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.
63+
Naturally, the browser probably didn't have time to load the script. As of now, the `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.
6364

6465
Let's add a `callback` function as a second argument to `loadScript` that should execute when the script loads:
6566

@@ -76,6 +77,8 @@ function loadScript(src, *!*callback*/!*) {
7677
}
7778
```
7879

80+
The `onload` event is described in the article <info:onload-onerror#loading-a-script>, it basically executes a function after the script is loaded and executed.
81+
7982
Now if we want to call new functions from the script, we should write that in the callback:
8083

8184
```js
@@ -101,7 +104,7 @@ function loadScript(src, callback) {
101104
*!*
102105
loadScript('https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.2.0/lodash.js', script => {
103106
alert(`Cool, the script ${script.src} is loaded`);
104-
alert( _ ); // function declared in the loaded script
107+
alert( _ ); // _ is a function declared in the loaded script
105108
});
106109
*/!*
107110
```

0 commit comments

Comments
 (0)