Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Node.js 7 what is the proper way to suppress UnhandledPromiseRejectionWarning?

In Node.js I have a module which consists of just one function. The function returns promise and the promise could be rejected. Still I don't want to force all users of the module to handle the rejection explicitly. By design in some cases it makes sense to just ignore the returned promise. Also I don't want to take the ability to handle the promise rejection away from module users.

What is the way to properly do so?

After upgrading to Node.js 7.1.0 all my unit tests which ignore rejection handling show the following warning:

(node:12732) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: try to throw an error from unit test
(node:12732) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

What is the proper way to prevent termination of Node.js process in the future mentioned in DeprecationWarning description?

like image 321
Volodymyr Frolov Avatar asked Nov 09 '16 20:11

Volodymyr Frolov


People also ask

How do you handle Unhandledpromiserejectionwarning?

You can handle this error in several ways. First, you can call on the try/catch block, or . catch() failure handler. Second, you can choose to use the unhandledrejection event handler to accomplish this.

What is Unhandledpromiserejectionwarning?

The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value. The Promise. reject() method returns a Promise object that is rejected with a given reason.

What is -- unhandled rejections strict?

August 22, 2021. Node. js has a CLI option called --unhandled-rejections=strict , which causes the script to completely fail instead of just throwing a warning when a JavaScript promise runs into an error.

What is process in node JS?

The process object in Node. js is a global object that can be accessed inside any module without requiring it. There are very few global objects or properties provided in Node. js and process is one of them. It is an essential component in the Node.


2 Answers

In general, using a custom library like bluebird you can suppress rejections just from your code but nowhere else. Native promises can't do this yet.

You can however manually suppress a promise by adding a catch handler for it.

 function yourExportedFunction() {
     const p = promiseThatMightRejectFn(); 
     p.catch(() => {}); // add an empty catch handler
     return p;
 }

This way you are explicitly ignoring the rejection from the promise so it is no longer an unhandled rejection just a suppressed one.

like image 81
Benjamin Gruenbaum Avatar answered Oct 21 '22 21:10

Benjamin Gruenbaum


If you're concerned about unhandled rejections causing your Nodejs process to terminate unintentionally in the future, you could register an event handler for the 'unhandledRejection' event on the process object.

process.on('unhandledRejection', (err, p) => {
  console.log('An unhandledRejection occurred');
  console.log(`Rejected Promise: ${p}`);
  console.log(`Rejection: ${err}`);
});

Edit

If you want the implementing user of your module to decide whether or not to handle the error in their code, you should just return your promise to the caller.

yourModule.js

function increment(value) {
  return new Promise((resolve, reject) => {
    if (!value)
      return reject(new Error('a value to increment is required'));                  

    return resolve(value++);
  });
}

theirModule.js

const increment = require('./yourModule.js');

increment()
  .then((incremented) => {
    console.log(`Value incremented to ${incremented}`);
  })
  .catch((err) => {
    // Handle rejections returned from increment()
    console.log(err);
  });
like image 25
peteb Avatar answered Oct 21 '22 21:10

peteb