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

后端 未结 4 941
谎友^
谎友^ 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

    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.

提交回复
热议问题