user193476
user193476

Reputation:

jQuery Deferreds, .pipe and exceptions

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.

Upvotes: 2

Views: 660

Answers (1)

glortho
glortho

Reputation: 13198

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.

Upvotes: 2

Related Questions