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

In Node.js I have a module that consists of only one function. The function returns a promise, and the promise may be rejected. However, I do not want all users of the module to handle the rejection explicitly. By design, in some cases it makes sense to simply ignore the returned promise. In addition, I do not want to use the opportunity to reject promises from users of modules.

How to do it right?

After upgrading to Node.js 7.1.0, all of my unit tests that ignore rejection processing 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. 

How can I prevent the completion of the Node.js process in the future mentioned in the DeprecationWarning description?

+5
source share
4 answers

In general, using a custom library such as bluebird, you can suppress deviations only from your code, but nowhere else. Native promises cannot do this yet.

However, you can 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; } 

Thus, you are clearly ignoring the rejection of the promise, so it is no longer an unworked rejection, only suppressed.

+4
source

If you are concerned about an unhandled rejection that caused the Nodejs process to end unexpectedly in the future, you can register an event handler for the 'unhandledRejection' event in 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 executive user of your module to decide whether to handle the error in your code, you should simply 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); }); 
+5
source

This is not your problem.

Just use promises the way they were intended. If the end user does not want to handle all the deviations, then they should add an unhandledRejection handler. Otherwise, they will need to add catches.

If your mistakes really are not violated, you should not refuse them. Just resolve with the error value. eg:

Success: resolve({result, error:null})

Failure: resolve({result:null, error})

It’s still better to just give up and leave the end user to decide how to deal with him.

+1
source

I cannot find a way to do what you are describing.

If you don't care about passing errors to your users, you can add a dummy catch block at the end of your promise chain:

 Promise.reject('foo') .catch(() => {}) 

This will turn off the warning, but will not allow your users to handle this error.

Perhaps you could add an option that your users could decide if they want to handle this error or not.

0
source

Source: https://habr.com/ru/post/1259522/


All Articles