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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 4

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Awesome!

Completion rate improved to 7.69

bookErrors in Promises

Svep för att visa menyn

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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 4
some-alt