Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer 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

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 4

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Awesome!

Completion rate improved to 7.69

bookErrors in Promises

Veeg om het menu te tonen

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

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 4
some-alt