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); });
peteb source share