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/03-promise-chaining/article.md
+3-2
Original file line number
Diff line number
Diff line change
@@ -69,8 +69,9 @@ new Promise(function(resolve, reject) {
69
69
70
70
The value returned by `.then` is a promise, that's why we are able to add another `.then` at `(2)`. When the value is returned in `(1)`, that promise becomes resolved, so the next handler runs with the value.
71
71
72
-
Please note: technically we can also add many `.then` to a single promise. This is not chaining:
72
+
**A classic newbie error: technically we can also add many `.then` to a single promise. This is not chaining.**
73
73
74
+
For example:
74
75
```js run
75
76
let promise =newPromise(function(resolve, reject) {
76
77
setTimeout(() =>resolve(1), 1000);
@@ -92,7 +93,7 @@ promise.then(function(result) {
92
93
});
93
94
```
94
95
95
-
What we did here is just several handlers to one promise. They don't pass the result to each other, instead they process it idependantly.
96
+
What we did here is just several handlers to one promise. They don't pass the result to each other, instead they process it independently.
96
97
97
98
Here's the picture (compare it with the chaining above):
Asynchronous tasks need proper management. For that, the standard specifies an internal queue `PromiseJobs`, more often called the "microtask queue" (v8 term).
26
+
Asynchronous tasks need proper management. For that, the standard specifies an internal queue `PromiseJobs`, more often referred to as "microtask queue" (v8 term).
27
27
28
28
As said in the [specification](https://tc39.github.io/ecma262/#sec-jobs-and-job-queues):
29
29
30
-
- The queue is first-in-first-out: tasks that get enqueued first are run first.
30
+
- The queue is first-in-first-out: tasks enqueued first are run first.
31
31
- Execution of a task is initiated only when nothing else is running.
32
32
33
33
Or, to say that simply, when a promise is ready, its `.then/catch/finally` handlers are put into the queue. They are not executed yet. Javascript engine takes a task from the queue and executes it, when it becomes free from the current code.
@@ -58,7 +58,7 @@ Now the order is as intended.
58
58
59
59
Browser Javascript, as well as Node.js, is based on an *event loop*.
60
60
61
-
"Event loop" is a process when the engine sleeps waits for events.
61
+
"Event loop" is a process when the engine sleeps and waits for events, then reacts on those and sleeps again.
62
62
63
63
Examples of events:
64
64
-`mousemove`, a user moved their mouse.
@@ -71,9 +71,11 @@ Things happen -- the engine handles them -- and waits for more to happen (while
71
71
72
72

73
73
74
-
When an event happens, and the engine is busy, it gets into a so-called "macrotask queue" (v8 term).
74
+
As you can see, there's also a queue here. A so-called "macrotask queue" (v8 term).
75
75
76
-
For instance, while the engine is busy processing a network `fetch`, a user may move their mouse causing `mousemove`, and `setTimeout` is due and so on, just as painted on the picture above.
76
+
When an event happens, and the engine is busy, the event is enqueued.
77
+
78
+
For instance, while the engine is busy processing a network `fetch`, a user may move their mouse causing `mousemove`, and `setTimeout` may be due and so on, just as painted on the picture above.
77
79
78
80
Events from the macrotask queue are processed on "first came – first served" basis. When the engine browser finishes with `fetch`, it handles `mousemove` event, then `setTimeout` handler, and so on.
79
81
@@ -120,20 +122,24 @@ Promise.resolved()
120
122
121
123
Naturally, `promise` shows up first, because `setTimeout` macrotask awaits in the less-priority macrotask queue.
122
124
125
+
**As a side effect, macrotasks are handled only when promises give the engine a "free time".**
126
+
127
+
So call have a promise chain that doesn't wait for anything, then things like `setTimeout` or event handlers can never get in the middle.
128
+
123
129
## Summary
124
130
125
-
Promise handling is always asynchronous, as all promise actions pass through the internal "promise jobs" queue, also called "microtask queue" (v8 term).
131
+
-Promise handling is always asynchronous, as all promise actions pass through the internal "promise jobs" queue, also called "microtask queue" (v8 term).
126
132
127
-
**So, `.then/catch/finally` is called after the current code is finished.**
133
+
**So, `.then/catch/finally` is called after the current code is finished.**
128
134
129
-
If we need to guarantee that a piece of code is executed after `.then/catch/finally`, it's best to add it into a chained `.then` call.
135
+
If we need to guarantee that a piece of code is executed after `.then/catch/finally`, it's best to add it into a chained `.then` call.
130
136
131
-
There's also a "macrotask queue" that keeps various events, network operation results, `setTimeout`-scheduled calls, and so on. These are also called "macrotasks" (v8 term).
137
+
-There's also a "macrotask queue" that keeps various events, network operation results, `setTimeout`-scheduled calls, and so on. These are also called "macrotasks" (v8 term).
132
138
133
-
The engine uses the macrotask queue to handle them in the appearance order.
139
+
The engine uses the macrotask queue to handle them in the appearance order.
134
140
135
-
**Macrotasks run after the code is finished *and* after the microtask queue is empty.**
141
+
**Macrotasks run after the code is finished *and* after the microtask queue is empty.**
136
142
137
-
In other words, they have lower priority.
143
+
In other words, they have lower priority.
138
144
139
-
So the order is: regular code, then promise handling, then everything else.
145
+
So the order is: regular code, then promise handling, then everything else, like events etc.
Copy file name to clipboardExpand all lines: 2-ui/5-data-storage/03-indexeddb/article.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ libs:
7
7
8
8
IndexedDB is a built-in database, much more powerful than `localStorage`.
9
9
10
-
-Also key/value storage, but not only strings: value can be (almost) anything, multiple key types.
10
+
-Key/value storage not only strings: value can be (almost) anything, multiple key types.
11
11
- Supports transactions for reliability.
12
12
- Supports key range queries, indexes.
13
13
- Can store much more data than `localStorage`.
@@ -16,7 +16,7 @@ That power is usually excessive for traditional client-server apps. IndexedDB is
16
16
17
17
The native interface to IndexedDB, described in the specification <https://www.w3.org/TR/IndexedDB>, is event-based.
18
18
19
-
We can also use `async/await`-based interface with the help of a promise-based wrapper, like <https://github.com/jakearchibald/idb>. That's really convenient, but the wrapper is not perfect, it can't replace events for all cases, so we'll start with events, and then use the wrapper.
19
+
We can also use `async/await`with the help of a promise-based wrapper, like <https://github.com/jakearchibald/idb>. That's pretty convenient, but the wrapper is not perfect, it can't replace events for all cases, so we'll start with events, and then use the wrapper.
0 commit comments