Skip to content

Commit 75895f4

Browse files
committed
up
1 parent 543680a commit 75895f4

File tree

10 files changed

+60
-83
lines changed

10 files changed

+60
-83
lines changed

1-js/04-object-basics/03-symbol/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ let user = {
113113
for(let key in user) alert(key); // name, age (no symbols)
114114
*/!*
115115

116-
// the direct access by the global symbol works
117-
alert( "Direct: " + user[Symbol.for("id")] );
116+
// the direct access by the symbol works
117+
alert( "Direct: " + user[id] );
118118
```
119119

120120
That's a part of the general "hiding" concept. If another script or a library loops over our object, it won't unexpectedly access a symbolic property.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The output is: `1`.
2+
3+
The second call to `resolve` is ignored, because only the first call of `reject/resolve` is taken into account. Further calls are ignored.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
# Re-resolve a promise?
3+
4+
5+
What's the output of the code below?
6+
7+
```js
8+
let promise = new Promise(function(resolve, reject) {
9+
resolve(1);
10+
11+
setTimeout(() => resolve(2), 1000);
12+
});
13+
14+
promise.then(alert);
15+
```

8-async/02-promise-basics/article.md

Lines changed: 26 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
# Promise
22

3-
4-
```compare plus="Plus" minus="Minus"
5-
+ One
6-
- two
7-
```
8-
93
A promise is an object of the built-in `Promise` class.
104

115
The promise object has two main internal properties: `state` and the `result`.
@@ -224,98 +218,59 @@ promise.then(
224218
promise.then(script => alert('One more handler to do something else!'));
225219
```
226220
227-
We can immediately see few benefits over the callback-based syntax in the example above.
228-
229-
| Callbacks | Promises |
230-
|-----------|----------|
231-
| We must have `callback` function when calling `loadScript`. So we must know what to do with the result *before* we make a call for it. | Promises allow us to code more naturally. First we run `loadScript`, then code what we do with the result. |
232-
|There can be only one callback. | We can call `.then` as many times as we want. |
221+
We can immediately see few benefits over the callback-based syntax:
233222
223+
```compare minus="Callbacks" plus="Promises"
224+
- We must have `callback` function available when calling `loadScript`. So we must know what to do with the result *before* we make a call for it.
225+
- There can be only one callback.
226+
+ Promises allow us to code more naturally. First we run `loadScript`, and `.then` code what we do with the result.
227+
+ We can call `.then` as many times as we want.
228+
```
234229
235-
We can call `promise.then` at any time. Maybe much later, when we really need that script.
236-
237-
1. We can call `promise.then` as many times as want, so we can add any number of handlers.
238-
3. The `promise` object can be passed around, new handlers can be added where needed in other parts of the code.
239-
240-
So promises give us flexibility. But there's more. We can chain promises and use `async` functions and so on. We'll see that in the next chapters.
241-
242-
Read the notes below for better understanding of promise objects.
243230
244-
````warn header="Once a promise settles, it can't be changed"
245-
When either `resolve` or `reject` is called -- the state change is final. The argument of `resolve/reject` is saved in the promise object.
231+
So promises give us better code flow and flexibility. But there's more. We'll see that in the next chapters.
246232
247-
Future calls of `resolve/reject` are ignored, so there's no way to "re-resolve" or "re-reject" a promise.
233+
````smart header="On settled promises `then` runs immediately"
234+
We can add `.then/catch` at any time.
248235
249-
For instance, here only the first `resolve` works:
236+
If the promise has not settled yet, then it will wait for the result. Otherwise, if the promise has already settled, then `promise.then/catch` handlers run immediately:
250237
251238
```js run
252-
let promise = new Promise(function(resolve, reject) {
253-
resolve("done!"); // immediately fulfill with the result: "done"
254-
255-
// a subsequent resolve is ignored
256-
setTimeout(() => resolve("..."), 1000);
257-
// a subsequent reject is ignored
258-
setTimeout(() => reject(new Error("..."), 2000));
259-
});
260-
261-
promise.then(result => alert(result), () => alert("Never runs"));
262-
```
263-
````
264-
239+
let promise = new Promise(resolve => resolve("done!")); // immediately resolve
265240
266-
````smart header="A promise may resolve immediately, doesn't have to be asynchronous"
267-
As we've seen from the example above, a promise may call resolve/reject without delay:
268-
269-
```js run
270-
new Promise(function(resolve, reject) {
271-
resolve("done!"); // immediately fulfill with the result: "done"
272-
}).then(alert);
241+
promise.then(alert); // done! (without delay)
273242
```
274243
275-
That's normal, sometimes it turns out that there's no asynchronous job to be done. For instance, if we have a cached result.
276-
````
277-
278-
````smart header="On settled promises `then` runs immediately"
279-
We can add `.then/catch` at any time.
280244
281-
If the promise has not settled yet, then it will wait for the result.
245+
To be precise -- handlers are always asynchronous, but if the promise is settled they run as soon as possible, similar to `setTimeout(...,0)`.
282246
283-
Otherwise, if the promise has already resolved/rejected, then subsequent `promise.then/catch` handlers are executed immediately.
284-
285-
To be precise -- they are still asynchronous, but run as soon as possible, similar to `setTimeout(...,0)`.
286-
287-
For instance, here we'll first see "code end", and then "done!":
247+
For instance, here we'll first see "code end" `(1)`, and then "done!" `(2)`:
288248
289249
```js run
290-
// the promise is in the "resolved" state immediately
291-
let promise = new Promise(function(resolve, reject) {
292-
resolve("done!");
293-
});
250+
let promise = new Promise(resolve => resolve("done!"));
294251
295-
// the handler runs as soon as possible, but asynchronously, like setTimeout(...,0)
296-
promise.then(alert);
252+
// alert runs as soon as possible, but asynchronously,
253+
// like if it were wrapped in setTimeout(..., 0);
254+
promise.then(alert); // (2)
297255
298-
alert('code end');
256+
alert('code end'); // (1)
299257
```
300258
301259
````
302260

303-
````smart header="Functions resolve/reject have only one argument"
261+
````smart header="Functions resolve/reject accept at most one argument"
304262
Functions `resolve/reject` accept only one argument.
305263
306-
We can call them without any arguments too. The call `resolve()` makes the result `undefined`:
264+
We can call them without any arguments too, that's the same as passing `undefined`:
307265
308266
```js run
309-
let promise = new Promise(function(resolve, reject) {
310-
resolve();
311-
});
267+
let promise = new Promise(resolve => resolve());
312268
313-
promise.then(result => alert(result)); // undefined
269+
promise.then(alert); // undefined
314270
```
315271
316-
...But if we pass many arguments: `resolve(1, 2, 3)`, then all arguments after the first one are ignored. A promise may have only one value as a result/error.
317-
318-
Use objects and destructuring if you need to pass many values, like this:
272+
...But if we pass many arguments: `resolve(1, 2, 3)`, then all arguments after the first one are ignored.
273+
The idea is that a promise may have only one result (or an error). Use objects and destructuring if you need to pass many values, like this:
319274
320275
```js run
321276
let promise = new Promise(function(resolve, reject) {

8-async/03-promise-chaining/article.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@
33

44
Let's formulate the problem mentioned in the chapter <info:callback-hell>:
55

6-
- We have a sequence of tasks to be done one after another. For instance, loading scripts. The next task may need the result of the previous one.
6+
- We have a sequence of asynchronous tasks to be done one after another. For instance, loading scripts.
77
- How to code it well?
88

9-
Promises can cover that need in two ways:
9+
Promises allow a couple of recipes to do that.
1010

11-
1. Promises chaining.
12-
2. Async functions.
11+
[cut]
1312

14-
Let's see the first way in this chapter and the second one in the next.
15-
16-
Promises chaining looks like this:
13+
In this chapter we cover promise chaining. It looks like this:
1714

1815
```js run
1916
new Promise(function(resolve, reject) {
@@ -36,12 +33,17 @@ new Promise(function(resolve, reject) {
3633
return result * 2;
3734

3835
});
39-
// ...
4036
```
4137

42-
As we can see, a call to `promise.then` returns a promise, that we can use again for `.then`. A value returned by `.then` becomes a result in the next `.then`. So in the example above we have a sequence of results: `1` -> `2` -> `4`.
38+
As we can see:
39+
- A call to `promise.then` returns a promise, that we can use for the next `.then` (chaining).
40+
- A value returned by `.then` handler becomes a result in the next `.then`.
41+
42+
![](promise-then-chain.png)
4343

44-
Please note that chaining `.then` is not the same as many `.then` on a single promise, like below:
44+
So in the example above we have a sequence of results: `1` -> `2` -> `4`.
45+
46+
Please note the difference between chained `.then` and many `.then` on a single promise, like below:
4547

4648
```js run
4749
let promise = new Promise(function(resolve, reject) {
@@ -66,6 +68,8 @@ promise.then(function(result) {
6668

6769
In the code above, all `.then` are on the same promise, so all of them get the same result -- the result of that promise. And all `alert` show the same: 1.
6870

71+
![](promise-then-many.png)
72+
6973
If we want to use the value returned by a handler of `.then`, then we should add a new `.then` after it (to chain).
7074

7175
## Returning promises
8.18 KB
Loading
19.8 KB
Loading
6.77 KB
Loading
16.2 KB
Loading

figures.sketch

-57.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)