1 year ago

#374143

test-img

YAHsaves

Is it possible to prevent JavaScript promises from handling exceptions/errors

I have a problem with JavaScript promises. Many times I intentionally "reject" them, and use the catch statement to process the rejected logic.

However the catch statement also triggers when there is a unhandled exception in my code, and I didn't mean to "reject" the promise.

I'm usually not console logging whatever comes into the catch function, and now my code just dies silently.

Is there any way to avoid this, and tell JavaScript to still console log these errors without console logging my own catch variables?

Edit to provide code example:

// Promise Function
function isEven(number){
  return new Promise((resolve, reject) => {
      if(number % 2 == 0){ resolve(); return; }
      reject();
  });
}

// Call Promise with logic handling
isEven(2).then(function(){

}).catch(function(err){

})

This works great to know if the number is even, but if I send an object by mistake instead of a number the code just silently dies. I know in this case I can test the catch response (err) for null as that's the "rejected" value, but do I have to custom filter this type of logic every time?

javascript

error-handling

promise

es6-promise

0 Answers

Your Answer

Accepted video resources