Can I prevent that Chrome (v45) pauses on promise rejections?

11,937

Solution 1

Chrome only does this if you have "pause on uncaught exception" turned on in the "Sources" tab.

enter image description here

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

Solution 2

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.

Share:
11,937
Mikael Sundberg
Author by

Mikael Sundberg

Geek developer, intrested in software architecture and is coding for fun.

Updated on June 07, 2022

Comments

  • Mikael Sundberg
    Mikael Sundberg almost 2 years

    If I have the developer tools visible in Chrome and a Promise is rejected, then Chrome pauses the javascript execution with the message "Paused on promise rejection". Can I in some way prevent Chrome from pause in this case (and still have devtools open)?

    Rejected promises is a part of the "normal" flow in my application and it's inconvenient to press the resume button in Chrome every time it happens.

    You can test this behaviour in Chrome by entering the following in the js-console:

    new Promise(function(accept, reject) { reject(); }) // (tested in v 45.0.2454.99)
    

    Thanks.

  • Mikael Sundberg
    Mikael Sundberg over 8 years
    Thanks, that solved most of my trouble. But your 'catch all' still causes Chrome to pause if "Pause on exceptions" is active.
  • Benjamin Gruenbaum
    Benjamin Gruenbaum over 8 years
    @MikaelSundberg then that's definitely a bug, I'll contact a friend from the devtools team and get it sorted. Thanks.
  • Benjamin Gruenbaum
    Benjamin Gruenbaum over 8 years
  • Bergi
    Bergi over 8 years
    @BenjaminGruenbaum: I don't think that's a bug. The debugger also pauses on thrown exceptions, regardless if they are caught or not.
  • Benjamin Gruenbaum
    Benjamin Gruenbaum over 8 years
    @Bergi only if you have "pause on caught exceptions" ticked.
  • Curtis
    Curtis over 5 years
    But I want to pause on my actual errors, just not this. Putting try/catch around it does nothing. This happens on audio.play() because the browser complains that you can't play audio until you've clicked something. There's no way to check if the browser is going to throw this.
  • Benjamin Gruenbaum
    Benjamin Gruenbaum over 5 years
    @Curtis please read the answer again more carefully, putting try/catch (without an await) won't help. You need to .catch on the play promise. audio.play().catch(e => doSomethingWithTheError(e))
  • Curtis
    Curtis over 5 years
    Is that for new browsers only that will return anything for audio.play? I guess you put another try/catch around the whole thing too to catch undefined.catch(). w3schools.com/jsref/met_audio_play.asp
  • trusktr
    trusktr about 5 years
    I wish there was an option not to pause on these, as I want to see how the rejections are handled (which do become uncaught errors in await statements, etc), so pausing on the reject is like pausing on the new Error call even if it isn't thrown there, which I don't care about.
  • Benjamin Gruenbaum
    Benjamin Gruenbaum about 5 years
    Rejecting a promise synchronously has exactly the same behavior as rejecting it asynchronously. The difference you are seeing is Chrome's "pause on uncaught exception" thinking the promise doesn't have a rejection handler attached when it is rejected.
  • mems
    mems over 4 years
    It's not possible to use this way to handle APIs that use Promise synchronous rejection. Ex: navigator.serviceWorker.register("https://example.com").catc‌​h(()=>Promise.resolv‌​e()) or new WritableStream().getWriter().releaseLock().catch(()=>Promise‌​.resolve())
  • chpio
    chpio over 4 years
    @mems have you tried Promise.resolve().then(() => navigator.serviceWorker.register("https://example.com")).cat‌​ch(console.error); ?
  • mems
    mems over 4 years
    The debugger still paused ("Pause on promise rejection") if you enable "Pause on exceptions" + uncheck "Pause on caught exceptions" (Chrome 77)
  • Curtis
    Curtis over 3 years
    I put it in try/catch, and used audio.play().catch() and it still pauses in Chrome! How do I solve this? I don't want my site to pause every time just because my debugger is open, but I do want it to pause if there's an actual error in my code like an undefined variable.