Errors 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.
12345678910111213const 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); });
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.
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
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
Errors 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.
12345678910111213const 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); });
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.
Merci pour vos commentaires !