Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Errors in Promises | Creating and Managing Custom Errors
Error Handling in JavaScript

bookErrors in Promises

When working with asynchronous code in JavaScript, promises provide a powerful way to manage operations that complete in the future. Unlike synchronous code, errors in promises do not immediately propagate to the surrounding context. Instead, they are handled through promise chaining. If a promise is rejected, the rejection flows down the chain until it is caught by a .catch() handler. Using .catch() is essential for handling errors in asynchronous operations, preventing unhandled promise rejections, and ensuring that your application can respond gracefully to failures.

12345678910111213
const fetchData = () => { return new Promise((resolve, reject) => { reject(new Error("Failed to fetch data")); }); }; fetchData() .then(data => { console.log("Data:", data); }) .catch(error => { console.error("Caught error:", error.message); });
copy

In this example, the fetchData function returns a promise that simulates an asynchronous failure by calling reject. When fetchData() is called, its promise is rejected, and the error does not throw synchronously, instead, the rejection is handled by the .catch() method attached to the promise chain. This ensures that the error is caught asynchronously, allowing you to respond to failures without crashing your application.

question mark

How do you handle errors that occur within a promise chain in JavaScript?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 4

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Can you explain how promise chaining works with multiple `.then()` and `.catch()` handlers?

What happens if I don't include a `.catch()` handler in my promise chain?

Can you show an example of handling errors in async/await syntax?

Awesome!

Completion rate improved to 7.69

bookErrors in Promises

Glissez pour afficher le menu

When working with asynchronous code in JavaScript, promises provide a powerful way to manage operations that complete in the future. Unlike synchronous code, errors in promises do not immediately propagate to the surrounding context. Instead, they are handled through promise chaining. If a promise is rejected, the rejection flows down the chain until it is caught by a .catch() handler. Using .catch() is essential for handling errors in asynchronous operations, preventing unhandled promise rejections, and ensuring that your application can respond gracefully to failures.

12345678910111213
const fetchData = () => { return new Promise((resolve, reject) => { reject(new Error("Failed to fetch data")); }); }; fetchData() .then(data => { console.log("Data:", data); }) .catch(error => { console.error("Caught error:", error.message); });
copy

In this example, the fetchData function returns a promise that simulates an asynchronous failure by calling reject. When fetchData() is called, its promise is rejected, and the error does not throw synchronously, instead, the rejection is handled by the .catch() method attached to the promise chain. This ensures that the error is caught asynchronously, allowing you to respond to failures without crashing your application.

question mark

How do you handle errors that occur within a promise chain in JavaScript?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 4
some-alt