This commit is contained in:
Ilya Kantor 2017-04-09 23:17:57 +02:00
parent 011d72bf17
commit 1b03278014
6 changed files with 30 additions and 3 deletions

View file

@ -35,7 +35,7 @@ alert(window.innerHeight); // some number
## Document Object Model (DOM) ## Document Object Model (DOM)
The `document` object gives access to a page contents. We can change or create literally anything. The `document` object gives access to the page content. We can change or create literally anything.
For instance: For instance:
```js run ```js run

View file

@ -5,12 +5,39 @@ Many things that we do in JavaScript are asynchronous. We initiate a process, bu
The most obvious example is `setTimeout`, but there are others, like making network requests, performing animations and so on. The most obvious example is `setTimeout`, but there are others, like making network requests, performing animations and so on.
Let's see a couple of examples, so that we can discover a problem, and then solve it using "promises".
[cut] [cut]
## Callbacks ## Callbacks
Consider a function `loadScript` that loads a script:
```js
function loadScript(src) {
let script = document.createElement('script');
script.src = src;
}
```
When the `<script>` tag is created and `src` is assigned, the browser loads the script and executes it. So, the function works. We can use it like this:
```js
loadScript('/my/script.js');
```
The function is asynchronous: the script starts loading now, but finishes later, maybe after a few seconds. So, the question is: how can we track the load end? As of now, the function provides no such way.
We'd like to invoke our code after the script is loaded. One of the easiest ways is to add a second argument to `loadScript`: the function that would run on load end.
How can hook on "load completion"?
From ancient times, Javascript allowed to use callback functions for asynchronous
Most asychronous
In this chapter we cover how to write callback-based asynchronous code.
Let's see a couple of examples, so that we can discover a problem, and then solve it using "promises".
Remember resource load/error events? They are covered in the chapter <info:onload-onerror>. Remember resource load/error events? They are covered in the chapter <info:onload-onerror>.
Let's say we want to create a function `loadScript` that loads a script and executes our code afterwards. Let's say we want to create a function `loadScript` that loads a script and executes our code afterwards.