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

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 show me the code example for this Promise chaining?

Can you explain what happens if I add another `.then()` after the `.catch()`?

How does this compare to using async/await for the same logic?

Awesome!

Completion rate improved to 7.69

bookChaining and Error Handling with Promises

Glissez pour afficher le menu

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

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 4
some-alt