2

I have code that looks somewhat like this (less ridiculous, of course):

var d = $.Deferred();
d.pipe(function() {
    throw "a";
}).then(function() {
    console.log("good!");
}, function(e) {
    console.log("I want the exception here");
});
d.resolve();

The problem is that throwing an exception in a .pipe doneFilter does not seem to make jQuery consider it as a failure and it results in an uncaught exception in the doneFilter.

An alternative is to create a new deferred and throw a try-catch block around the doneFilter, but I was wondering if there was a better way to go about it.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
  • I'm confused because your example does not use either `.pipe()` or `doneFilter` which you then go on to talk about `\-:` – hippietrail Aug 08 '12 at 10:01

1 Answers1

2

If you have to use throw, it is indeed best to catch it in the doneFilter, and return $.Deferred().reject('a') (or whatever) from it on error. The fail callback in then will then be called with the arg you pass.

glortho
  • 13,120
  • 8
  • 49
  • 45