Promises and generic .catch() statements

后端 未结 3 2118
情话喂你
情话喂你 2021-01-04 17:47

I\'m writing an API for my system, that\'s sending an XHR to the server and returns a promise that should be handled by the caller - so far so good.

For each API cal

相关标签:
3条回答
  • 2021-01-04 18:10

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

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

    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.

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