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

后端 未结 4 887
谎友^
谎友^ 2021-01-04 10:26

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

相关标签:
4条回答
  • 2021-01-04 10:41

    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.

    0 讨论(0)
  • 2021-01-04 10:41

    This is not your problem to fix.

    Just use promises the way they were intended. If the end user doesn't want to handle all rejections then THEY have to add an unhandledRejection handler. Otherwise they will need to add catches.

    If your errors truly aren't breaking then you shouldn't be rejecting on them. Just resolve with an error value. e.g:

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

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

    It's still better to just reject and leave the end user to decide how to handle it.

    0 讨论(0)
  • 2021-01-04 10:45

    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);
      });
    
    0 讨论(0)
  • 2021-01-04 10:46

    I can't figure out any way to do what you're describing.

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

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

    This will silence the warning, but won't allow your users to handle the error.

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

    0 讨论(0)
提交回复
热议问题