Can I prevent that Chrome (v45) pauses on promise rejections?
Chrome only does this if you have "pause on uncaught exception" turned on in the "Sources" tab.
If you untick it it will not pause on errors.
A promise rejection is conceptually an error. It is the correct way to mentally model it, otherwise the following are silent errors:
Promise.resolve().then(function(){ JSON.prase("{}"); // unhandled rejection, TypeError, typo foooooo = 15; // unhandled ReferenceError, undefined});
And so on.
If you want to explicitly suppress a rejection, which is akin to a synchronous "catch all" you'd do the same thing you do in synchronous code:
try { doSomething();} catch(e){ // explicitly ignore all errors.}
With promises:
doSomething().catch(function(){}); // explicitly don't report rejection
It is interesting to notice that rejecting a promise syncronously throws an Error while asynchronously doesn't. Consider this example:
var willIGetNewPhone = ()=>new Promise( (resolve, reject)=>{ setTimeout(()=>{ var reason = new Error('mom is not happy'); reject(reason); }, 1000); });willIGetNewPhone() .catch(error=>{ console.log(error.message); });
This code doesn't throw exception. But if setTimeout part is removed the exception is thrown.
You can workaround this (as @mprcela has noted) by doing the rejection asynchronously. This is due to the fact, that we are allowed to add a catch
handler before the actual rejection is executed. Also it seems that it's not triggering a pause if there is a catch
handler on the outer Promise.
But it will still pause if the catch
isn't added synchronously.
I'm delaying the execution by wrapping the rejection in a then
block, this is due to that then
, catch
and finally
handlers (not the Promise
constructor tho) get queued up and executed delayed (microtask).
function loadData(url) { if (!url) { return Promise.resolve().then(() => Promise.reject('no url')); } return fetch(url) .then(res => { if (!res.ok) { // we are already in a then block return Promise.reject(res.statusText); } // parse response as json return res.json(); });}loadData() .then(() => { // ... }) .catch(console.log);