Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Handling Unhandled Promise Rejections | Async/Await and Best Practices for Asynchronous Code
Node.js Event Loop and Asynchronous Code

bookHandling Unhandled Promise Rejections

When working with Promises in Node.js, it is important to handle every possible rejection. If a Promise is rejected and there is no handler—such as a .catch() method or a try/catch block when using async/await—the rejection is considered unhandled. Unhandled Promise rejections can cause your application to behave unpredictably. In earlier versions of Node.js, unhandled rejections would only log a warning, but in more recent versions, they may cause the process to terminate. This can lead to downtime, lost data, or security risks if not addressed.

index.js

index.js

copy
Note
Note

In large Node.js applications, you can listen for unhandled Promise rejections globally to help with debugging or logging. This is especially useful in development environments.

process.on('unhandledRejection', (reason, promise) => {
  console.error('Unhandled Rejection:', reason);
});

This event lets you detect unhandled rejections, but it should not be used as a replacement for proper .catch() or try/catch handling. Always handle rejections explicitly in your code to prevent unexpected crashes in production.

question mark

What is a possible consequence of leaving a Promise rejection unhandled in recent versions of Node.js?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. 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:

What are some best practices for handling Promise rejections in Node.js?

Can you show examples of handling errors with async/await and Promises?

What happens if I ignore unhandled Promise rejections in my application?

Awesome!

Completion rate improved to 7.69

bookHandling Unhandled Promise Rejections

Glissez pour afficher le menu

When working with Promises in Node.js, it is important to handle every possible rejection. If a Promise is rejected and there is no handler—such as a .catch() method or a try/catch block when using async/await—the rejection is considered unhandled. Unhandled Promise rejections can cause your application to behave unpredictably. In earlier versions of Node.js, unhandled rejections would only log a warning, but in more recent versions, they may cause the process to terminate. This can lead to downtime, lost data, or security risks if not addressed.

index.js

index.js

copy
Note
Note

In large Node.js applications, you can listen for unhandled Promise rejections globally to help with debugging or logging. This is especially useful in development environments.

process.on('unhandledRejection', (reason, promise) => {
  console.error('Unhandled Rejection:', reason);
});

This event lets you detect unhandled rejections, but it should not be used as a replacement for proper .catch() or try/catch handling. Always handle rejections explicitly in your code to prevent unexpected crashes in production.

question mark

What is a possible consequence of leaving a Promise rejection unhandled in recent versions of Node.js?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 4
some-alt