Trap when uncontrolled deviations

I would like trap.js raw rejects so that I can register them. To accomplish this, I exceeded console.warn (), however it may write files other than when.js that are not of interest to me.

ref: https://github.com/cujojs/when/blob/master/docs/api.md#debugging-promises

I am using prettymonitor with if.js https://github.com/AriaMinaei/pretty-monitor

+6
source share
1 answer

If you are on the server side, you can use promise rejection traps. They will work with most server-side promise implementations (io.js, bluebird, when, etc.):

process.on("unhandledRejection", function(promise, reason){ // deal with the rejection here. }); 

If you are in a browser environment, everything becomes less standardized. However, when there are still similar hooks:

 window.addEventListener('unhandledRejection', function(event) { event.preventDefault(); // This stops the initial log. // handle event event.detail.reason; // rejection reason event.detail.key; // rejection promise key }, false); 

There are also local rollback hooks, they are good if you want to handle the deviations of one instance of the promise library - this is usually useful when creating the library yourself:

 var Promise = require('when').Promise; Promise.onPotentiallyUnhandledRejection = function(rejection) { // handle single instance error here }; 
+4
source

Source: https://habr.com/ru/post/985317/


All Articles