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?
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.
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.
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.
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.
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.
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}`);
});
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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With