Skip to content

Commit 840b7c5

Browse files
committed
add asynchronous chapter to error handling
1 parent 5367e67 commit 840b7c5

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

en/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
- [Error Handling](error-handling/README.md)
7474
- [Try...Catch](error-handling/try...-catch.md)
7575
- [Try...Catch...Finally](error-handling/try...catch...finally.md)
76+
- [Asynchronous Error Handling](error-handling/async_errorhandling.md)
7677
- [Modules](modules.md)
7778
- [Regular Expression](regular-expression.md)
7879
- [Classes](classes/README.md)

en/error-handling/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ One of the common error handling techniques is the `try...catch` block, which is
5858
* [try...catch](./try...-catch.md)
5959
* [try...catch...finally](./try...catch...finally.md)
6060

61-
Error handling is a critical aspect of JavaScript development.
62-
By understanding the types of errors, and following best practices,
63-
you can write more reliable and user-friendly applications.
61+
## Asynchronous Error Handling
62+
63+
Handling errors in asynchronous code can be tricky. In synchronous code, errors are caught immediately, but with asynchronous operations like API calls, errors may occur later. You'll handle them in callbacks or promises, so you need to be prepared. We'll cover how to manage these errors using promises and `async/await` in the section below.
64+
65+
* [Asynchronous Error Handling](./async_errorhandling.md)
6466

6567

en/error-handling/async_errorhandling.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
1+
---
2+
chapter: 12
3+
pageNumber: 87
4+
description: Handling asynchronous errors is a bit more complex than synchronous errors. The issue is that the code that generates the error is not directly responsible for handling the error. Instead, the error will be handled by the callback function that is executed when the asynchronous operation is complete.
25
---
36

47
# Asynchronous Error Handling
@@ -10,7 +13,7 @@ The example below shows how you can handle asynchronous errors:
1013

1114
A common example is when using the `fetch` API to download some data from a server. The `fetch` API returns a promise that resolves with the server response. If the server returns an error, the promise will reject with the error.
1215

13-
### Example 1: Using `try...catch` with `async/await`
16+
#### Using `try...catch` with `async/await`
1417

1518
Using `async/await` makes asynchronous code look and behave more like synchronous code, which can make it easier to read and understand. Here's how you can handle errors using `try...catch`:
1619

@@ -30,7 +33,7 @@ async function fetchData(url) {
3033
```
3134

3235

33-
### Example 2: Handling errors with `.catch()` in Promises
36+
#### Handling errors with `.catch()` in Promises
3437
When using Promises directly, you can handle errors using the `.catch()` method:
3538

3639
```javascript
@@ -91,7 +94,7 @@ async function fetchData(url) {
9194
```
9295

9396

94-
### Graceful Degradation: Provide fallback behavior or user-friendly error messages.
97+
#### Graceful Degradation: Provide fallback behavior or user-friendly error messages.
9598

9699
```javascript
97100
async function fetchData(url) {
@@ -110,7 +113,7 @@ async function fetchData(url) {
110113
```
111114

112115

113-
### Logging: Log errors for debugging and monitoring purposes.
116+
#### Logging: Log errors for debugging and monitoring purposes.
114117

115118
```javascript
116119
async function fetchData(url) {
@@ -134,7 +137,7 @@ function logErrorToService(error) {
134137
```
135138

136139

137-
### Avoid Silent Failures: Ensure that errors are not swallowed silently; always log or handle them.
140+
#### Avoid Silent Failures: Ensure that errors are not swallowed silently; always log or handle them.
138141

139142

140143
```javascript
@@ -154,7 +157,7 @@ async function fetchData(url) {
154157

155158

156159

157-
### Centralized Error Handling: Consider using a centralized error handling mechanism for larger applications.
160+
#### Centralized Error Handling: Consider using a centralized error handling mechanism for larger applications.
158161

159162

160163
```javascript
@@ -177,8 +180,6 @@ function handleError(error) {
177180
}
178181
```
179182

180-
## Conclusion
181-
182183

183184
Proper error handling in asynchronous operations is crucial for building resilient JavaScript applications. By following the examples and best practices outlined in this guide, you can ensure that your code gracefully handles errors, provides meaningful feedback to users, and maintains overall application stability. Always remember to handle errors in every asynchronous operation, use try...catch with async/await for readability, and implement centralized error handling for larger applications. With these strategies, you can effectively manage asynchronous errors and create more reliable and user-friendly applications.
184185

0 commit comments

Comments
 (0)