Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Chaining and Error Handling with Promises | Asynchronous APIs and Promises
Node.js Event Loop and Asynchronous Code

bookChaining and Error Handling with Promises

Chaining Promises allows you to run asynchronous tasks in sequence, making your code more readable and manageable. Each .then() receives the resolved value from the previous Promise and can return a new Promise for the next step. If a Promise in the chain is rejected, the error skips the remaining .then() blocks and jumps directly to the nearest .catch().

index.js

index.js

copy

Here's a step-by-step breakdown of how Promise chaining and error handling work in the example:

  1. The fetchData function returns a Promise that resolves with "Data loaded" after a short delay;
  2. The first .then() logs this value and returns the result of processData(result), which is another Promise;
  3. The second .then() logs the processed data and returns the result of saveData(processed);
  4. The third .then() would log the saved data, but in this example, saveData always rejects with "Error saving data";
  5. When a Promise is rejected, the .catch() block is triggered, logging the error message;
  6. If an error is thrown or a Promise is rejected at any point in the chain, control passes to the nearest .catch(), and the rest of the .then() blocks are skipped.

This approach helps you handle asynchronous operations cleanly and ensures that errors are not missed, making your code more robust.

Note
Note

Always include a .catch() at the end of your Promise chain to handle any errors that occur in any part of the sequence.

question mark

What happens in a Promise chain if a Promise is rejected or an error is thrown before reaching a .catch() block?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 4

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Awesome!

Completion rate improved to 7.69

bookChaining and Error Handling with Promises

Stryg for at vise menuen

Chaining Promises allows you to run asynchronous tasks in sequence, making your code more readable and manageable. Each .then() receives the resolved value from the previous Promise and can return a new Promise for the next step. If a Promise in the chain is rejected, the error skips the remaining .then() blocks and jumps directly to the nearest .catch().

index.js

index.js

copy

Here's a step-by-step breakdown of how Promise chaining and error handling work in the example:

  1. The fetchData function returns a Promise that resolves with "Data loaded" after a short delay;
  2. The first .then() logs this value and returns the result of processData(result), which is another Promise;
  3. The second .then() logs the processed data and returns the result of saveData(processed);
  4. The third .then() would log the saved data, but in this example, saveData always rejects with "Error saving data";
  5. When a Promise is rejected, the .catch() block is triggered, logging the error message;
  6. If an error is thrown or a Promise is rejected at any point in the chain, control passes to the nearest .catch(), and the rest of the .then() blocks are skipped.

This approach helps you handle asynchronous operations cleanly and ensures that errors are not missed, making your code more robust.

Note
Note

Always include a .catch() at the end of your Promise chain to handle any errors that occur in any part of the sequence.

question mark

What happens in a Promise chain if a Promise is rejected or an error is thrown before reaching a .catch() block?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 4
some-alt