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/04-promise-error-handling/article.md
+5-5Lines changed: 5 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -76,7 +76,7 @@ new Promise((resolve, reject) => {
76
76
77
77
The "invisible `try..catch`" around the executor automatically catches the error and treats it as a rejection.
78
78
79
-
That's so not only in the executor, but in handlers as well. If we `throw` inside a `.then` handler, that means a rejected promise, so the control jumps to the nearest error handler.
79
+
This happens not only in the executor, but in its handlers as well. If we `throw` inside a `.then` handler, that means a rejected promise, so the control jumps to the nearest error handler.
80
80
81
81
Here's an example:
82
82
@@ -90,7 +90,7 @@ new Promise((resolve, reject) => {
90
90
}).catch(alert); // Error: Whoops!
91
91
```
92
92
93
-
That's so not only for `throw`, but for any errors, including programming errors as well:
93
+
This happens for all errors, not just those caused by the `throw` statement. For example, a programming error:
94
94
95
95
```js run
96
96
newPromise((resolve, reject) => {
@@ -102,7 +102,7 @@ new Promise((resolve, reject) => {
102
102
}).catch(alert); // ReferenceError: blabla is not defined
103
103
```
104
104
105
-
As a side effect, the final `.catch` not only catches explicit rejections, but also occasional errors in the handlers above.
105
+
The final `.catch` not only catches explicit rejections, but also occasional errors in the handlers above.
106
106
107
107
## Rethrowing
108
108
@@ -120,7 +120,7 @@ new Promise((resolve, reject) => {
120
120
121
121
thrownewError("Whoops!");
122
122
123
-
}).catch(function(error) {
123
+
}).catch(function(error) {
124
124
125
125
alert("The error is handled, continue normally");
126
126
@@ -266,7 +266,7 @@ new Promise(function() {
266
266
267
267
In case of an error, the promise state becomes "rejected", and the execution should jump to the closest rejection handler. But there is no such handler in the examples above. So the error gets "stuck".
268
268
269
-
In practice, just like with a regular unhandled errors, it means that something terribly gone wrong, the script probably died.
269
+
In practice, just like with a regular unhandled errors, it means that something has terribly gone wrong, the script probably died.
270
270
271
271
Most JavaScript engines track such situations and generate a global error in that case. We can see it in the console.
In other words, the `documentElement` (`<html>`) is the root node. Formally, it has `document` as its parent. But `document` is not an element node, so `parentNode` returns it and `parentElement` does not.
239
239
240
-
Sometimes that matters when we're walking over the chain of parents and call a method on each of them, but `document` doesn't have it, so we exclude it.
240
+
This loop travels up from an arbitrary element `elem` to `<html>`, but not to the `document`:
241
+
```js
242
+
while(elem =elem.parentElement) {
243
+
alert( elem ); // parent chain till <html>
244
+
}
245
+
```
241
246
````
242
247
243
248
Let's modify one of the examples above: replace `childNodes` with `children`. Now it shows only elements:
0 commit comments