---
title: "Handle \"UnhandledPromiseRejectionWarning\" in Node.js backend?"  
description: "Handle \"UnhandledPromiseRejectionWarning\" in Node.js backend?"  
author: "Sandra Emily"  
published: 2023-08-04  
updated: 2023-08-04  
canonical: https://www.mindstick.com/forum/159461/handle-unhandledpromiserejectionwarning-in-node-js-backend  
category: "node.js"  
tags: ["exception handling", "node js"]  
reading_time: 3 minutes  

---

# Handle "UnhandledPromiseRejectionWarning" in Node.js backend?

[Handle](https://www.mindstick.com/articles/311004/suede-skillet-handle-cover) "UnhandledPromiseRejectionWarning" in [Node.js](https://www.mindstick.com/articles/1499/upload-and-download-file-in-node-js) [backend](https://www.mindstick.com/articles/337404/how-to-build-and-consume-restful-apis-using-php)?

## Replies

### Reply by Aryan Kumar

There are two ways to handle unhandled promise rejections in Node.js:

- **Use the** `.catch()` **handler method:** This is the most common way to handle promise rejections. The `.catch()` method takes a function as its argument, which will be called if the promise is rejected. The function passed to `.catch()` can handle the error however you want, such as logging it to the console, returning an error message, or retrying the operation.

```plaintext
const myPromise = new Promise((resolve, reject) => {
  // Do something that could potentially fail
  if (somethingFailed()) {
    reject(new Error('Something went wrong'));
  } else {
    resolve('All good');
  }
});

myPromise.catch((err) => {
  console.log('An error occurred:', err);
});
```

- **Add a second function to the** `.then()` **handler method:** You can also handle promise rejections by adding a second function to the `.then()` handler method. The second function will be called if the promise is rejected. The second function can handle the error however you want, just like the `.catch()` method.

```plaintext
const myPromise = new Promise((resolve, reject) => {
  // Do something that could potentially fail
  if (somethingFailed()) {
    reject(new Error('Something went wrong'));
  } else {
    resolve('All good');
  }
});

myPromise.then((result) => {
  console.log('The promise was fulfilled with the result:', result);
}).catch((err) => {
  console.log('An error occurred:', err);
});
```

In addition to handling promise rejections, you can also use the `process.on('unhandledRejection')` event to listen for unhandled promise rejections. This event will be emitted whenever a promise is rejected and no handler has been attached to it. You can use this event to log the error to the console, return an error message, or retry the operation.

```plaintext
process.on('unhandledRejection', (err) => {
  console.log('An error occurred:', err);
});
```

It is important to handle unhandled promise rejections in your Node.js backend. If you don't, the Node.js process will terminate with a non-zero exit code. This can cause problems for your users and your applications.

Here are some additional tips for handling unhandled promise rejections in Node.js:

- Use the `.catch()` handler method whenever possible. This is the most common way to handle promise rejections and it is the most efficient.
- If you need to use the `.then()` handler method, add a second function to handle promise rejections. This will make your code more readable and maintainable.
- Use the `process.on('unhandledRejection')` event to listen for unhandled promise rejections. This will allow you to log the error to the console, return an error message, or retry the operation.
- Test your code thoroughly to make sure that all possible errors are handled.


---

Original Source: https://www.mindstick.com/forum/159461/handle-unhandledpromiserejectionwarning-in-node-js-backend

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
