Mathias Dolidon Mathias Dolidon - 1 month ago 4
Javascript Question

Can I reject a dojo/Deferred without getting an error in the browser console?

I'd like to be able to reject dojo/Deferred objects without this being shown as errors in the browser's console. Many of these rejections aren't actual errors. They're only rejections, part of processes that didn't follow their main path of execution but which didn't end up in a catastrophe either. I'm even more bothered about these spurious error reports since I capture the error logs and send them to a server for possible further analysis (large product in beta testing phase).

Has anyone done it ?

// assuming we're running in a Dojo environment

require(["dojo/Deferred"],
function(Deferred) {
var d = new Deferred();
d.reject("I don't want to see this on the console !");
});


PS : answers such as "write your own Deferred class by copying/changing the one from Dojo's open source code" wouldn't be the kind that I'm expecting. ;)

Answer

Sure, you just have to .catch() the rejections somewhere (and ignore them if you want). It's the same for synchronously thrown errors - if you don't wrap the code in a try/catch, the exceptions will show up in the console. You need to do that explicitly, because otherwise it will be considered an unexpected error/rejection and be logged as such.