I'm using Dexie.js for handling indexed storage. However, it would take over exception handling on any process launched by the deferred promise. This made it very difficult to catch the bugs where they happened.
2 Answers
This is what works. I would love to get input from other developers if this is a good idea.
function isolate(deferred) {
return {
exec: function() {
var args = arguments;
var timeout = setTimeout(function() {
deferred.apply(this, args);
clearTimeout(timeout);
},1);
}
};
}
function save(name, drawing, data, onComplete) {
return db.drawings.put(
{ name: name, drawing, data: data
).then(isolate(onComplete).exec);
}
This way the promise function completes, Dexie.js is very happy about it, and then a new one starts in the timeout, which Dexie (or any other promise style handler) doesn't care about anymore.

- 699
- 4
- 27
If you need to debug your code and break when an error occurs, you can do that also in Promise-based code without having to do your suggested workaround.
In the F12 debugger in Chrome, check "Pause on caught exceptions". Also make sure to blackbox 3rd part libs so you don't end up breaking on feature testings in babel, jquery, dexie. This is simply done by right-clicking somewhere on the script code (when debugger breaks into it) and choose "blackbox". (Make sure NOT to blackbox your own code though!)
Now the debugger will break when an error occur in your code no matter if it's launched from a promise callback.

- 5,058
- 1
- 20
- 19
-
Sure, that helps, but then you get a whole bunch of errors that were supposed to be caught and it makes the debugging harder – GeeWhizBang Nov 15 '16 at 19:06
-
I didn't know about the blackbox stuff, I'm going to try that. – GeeWhizBang Nov 15 '16 at 20:03