From f2c9b0c9e85e5cc40b0d87270f472ffc2da57a9b Mon Sep 17 00:00:00 2001 From: sumn2u Date: Sun, 13 Oct 2024 10:45:24 -0500 Subject: [PATCH] add asynchronous chapter to error handling --- en/SUMMARY.md | 1 + en/error-handling/README.md | 8 +++++--- en/error-handling/async_errorhandling.md | 19 ++++++++++--------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/en/SUMMARY.md b/en/SUMMARY.md index 87df426a..ab6b8abf 100644 --- a/en/SUMMARY.md +++ b/en/SUMMARY.md @@ -73,6 +73,7 @@ - [Error Handling](error-handling/README.md) - [Try...Catch](error-handling/try...-catch.md) - [Try...Catch...Finally](error-handling/try...catch...finally.md) + - [Asynchronous Error Handling](error-handling/async_errorhandling.md) - [Modules](modules.md) - [Regular Expression](regular-expression.md) - [Classes](classes/README.md) diff --git a/en/error-handling/README.md b/en/error-handling/README.md index e79981ef..ad8122e4 100644 --- a/en/error-handling/README.md +++ b/en/error-handling/README.md @@ -58,8 +58,10 @@ One of the common error handling techniques is the `try...catch` block, which is * [try...catch](./try...-catch.md) * [try...catch...finally](./try...catch...finally.md) -Error handling is a critical aspect of JavaScript development. -By understanding the types of errors, and following best practices, -you can write more reliable and user-friendly applications. +## Asynchronous Error Handling + +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. + +* [Asynchronous Error Handling](./async_errorhandling.md) diff --git a/en/error-handling/async_errorhandling.md b/en/error-handling/async_errorhandling.md index 9f4a245f..0423aa90 100644 --- a/en/error-handling/async_errorhandling.md +++ b/en/error-handling/async_errorhandling.md @@ -1,4 +1,7 @@ - +--- +chapter: 12 +pageNumber: 87 +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. --- # Asynchronous Error Handling @@ -10,7 +13,7 @@ The example below shows how you can handle asynchronous errors: 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. -### Example 1: Using `try...catch` with `async/await` +#### Using `try...catch` with `async/await` 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`: @@ -30,7 +33,7 @@ async function fetchData(url) { ``` -### Example 2: Handling errors with `.catch()` in Promises +#### Handling errors with `.catch()` in Promises When using Promises directly, you can handle errors using the `.catch()` method: ```javascript @@ -91,7 +94,7 @@ async function fetchData(url) { ``` -### Graceful Degradation: Provide fallback behavior or user-friendly error messages. +#### Graceful Degradation: Provide fallback behavior or user-friendly error messages. ```javascript async function fetchData(url) { @@ -110,7 +113,7 @@ async function fetchData(url) { ``` -### Logging: Log errors for debugging and monitoring purposes. +#### Logging: Log errors for debugging and monitoring purposes. ```javascript async function fetchData(url) { @@ -134,7 +137,7 @@ function logErrorToService(error) { ``` -### Avoid Silent Failures: Ensure that errors are not swallowed silently; always log or handle them. +#### Avoid Silent Failures: Ensure that errors are not swallowed silently; always log or handle them. ```javascript @@ -154,7 +157,7 @@ async function fetchData(url) { -### Centralized Error Handling: Consider using a centralized error handling mechanism for larger applications. +#### Centralized Error Handling: Consider using a centralized error handling mechanism for larger applications. ```javascript @@ -177,8 +180,6 @@ function handleError(error) { } ``` -## Conclusion - 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.