Promises and generic .catch() statements

蓝咒 提交于 2019-11-30 18:30:45

I think all you want to do is rethrow the exception after you've logged it so other handlers will deal with it properly:

return deferred.promise.catch(function (error) {
  console.error(error);
  throw e; // rethrow the promise
});

Q use NodeJS process to raise unhandledRejection. If you dont use NodeJS, you can use this Workaround:

// Simulating NodeJS process.emit to handle Q exceptions globally
process = {
    emit: function (event, reason, promise) {
        switch(event)
        {
            case 'unhandledRejection':
                console.error("EXCEPTION-Q> %s", reason.stack || reason)
                break;
        }
    }
}

With bluebird Promises, you can call

Promise.onPossiblyUnhandledRejection(function(error){
    // Handle error here
    console.error(error);
});

With iojs you have the process.on('unhandledRejection') handler as specified here. (Also worth reading this and this

As far as I know, neither native Promises anywhere else nor Q Promises offer this functionality.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!