Skip to content

Commit 0e97922

Browse files
committed
fixes
1 parent 8c96150 commit 0e97922

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

1-js/11-async/07-microtask-queue/article.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
Promise handlers `.then`/`.catch`/`.finally` are always asynchronous.
55

6-
Even when a Promise is immediately resolved, the code on the lines *below* your `.then`/`.catch`/`.finally` will still execute first.
6+
Even when a Promise is immediately resolved, the code on the lines *below* `.then`/`.catch`/`.finally` will still execute before these handlers .
77

8-
Here's the code that demonstrates it:
8+
Here's the demo:
99

1010
```js run
1111
let promise = Promise.resolve();
@@ -21,7 +21,7 @@ That's strange, because the promise is definitely done from the beginning.
2121

2222
Why did the `.then` trigger afterwards? What's going on?
2323

24-
# Microtasks
24+
## Microtasks
2525

2626
Asynchronous tasks need proper management. For that, the standard specifies an internal queue `PromiseJobs`, more often referred to as "microtask queue" (v8 term).
2727

@@ -54,9 +54,11 @@ Now the order is as intended.
5454

5555
## Event loop
5656

57-
In-browser JavaScript, as well as Node.js, is based on an *event loop*.
57+
In-browser JavaScript execution flow, as well as Node.js, is based on an *event loop*.
5858

59-
"Event loop" is a process when the engine sleeps and waits for events, then reacts on those and sleeps again.
59+
"Event loop" is a process when the engine sleeps and waits for events. When they occur - handles them and sleeps again.
60+
61+
Events may come either comes from external sources, like user actions, or just as the end signal of an internal task.
6062

6163
Examples of events:
6264
- `mousemove`, a user moved their mouse.
@@ -120,14 +122,14 @@ Promise.resolve()
120122

121123
Naturally, `promise` shows up first, because `setTimeout` macrotask awaits in the less-priority macrotask queue.
122124

123-
As a logical consequence, macrotasks are handled only when promises give the engine a "free time". So if we have a promise chain that doesn't wait for anything, then things like `setTimeout` or event handlers can never get in the middle.
125+
As a logical consequence, macrotasks are handled only when promises give the engine a "free time". So if we have a chain of promise handlers that don't wait for anything, execute right one after another, then a `setTimeout` (or a user action handler) can never run in-between them.
124126

125127

126128
## Unhandled rejection
127129

128130
Remember "unhandled rejection" event from the chapter <info:promise-error-handling>?
129131

130-
Now, with the understanding of microtasks, we can formalize it.
132+
Now we can describe how JavaScript finds out that a rejection was not handled.
131133

132134
**"Unhandled rejection" is when a promise error is not handled at the end of the microtask queue.**
133135

@@ -167,15 +169,15 @@ setTimeout(() => promise.catch(err => alert('caught')));
167169
window.addEventListener('unhandledrejection', event => alert(event.reason));
168170
```
169171

170-
Now the unhandled rejection appears again. Why? Because `unhandledrejection` triggers when the microtask queue is complete. The engine examines promises and, if any of them is in "rejected" state, then the event is generated.
172+
Now the unhandled rejection appears again. Why? Because `unhandledrejection` is generated when the microtask queue is complete. The engine examines promises and, if any of them is in "rejected" state, then the event triggers.
171173

172174
In the example, the `.catch` added by `setTimeout` triggers too, of course it does, but later, after `unhandledrejection` has already occurred.
173175

174176
## Summary
175177

176178
- Promise handling is always asynchronous, as all promise actions pass through the internal "promise jobs" queue, also called "microtask queue" (v8 term).
177179

178-
**So, `.then/catch/finally` are called after the current code is finished.**
180+
**So, `.then/catch/finally` handlers are called after the current code is finished.**
179181

180182
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.
181183

0 commit comments

Comments
 (0)