I found out today that Deferred.then(null,func) and Deferred.fail(func) aren't the same thing in JQuery. In ES6's promise, Promise.then(null,func) and Promise.catch(func) are the same thing, so I was confused by JQuery's functions.
The only difference I know of is this:
$.Deferred().reject().promise()
.fail(function(){
return $.Deferred().resolve().promise();
})
.then(function(){
console.log('fail caught error'); // NOT printed
});
$.Deferred().reject().promise()
.then(null,function(){
return $.Deferred().resolve().promise();
})
.then(function(){
console.log('then caught error'); //printed
});
Are there any other useful differences?
Yes, the difference between them is that
.fail()does return the original promise it was called upon while.then()does construct a new promise which might resolve with a different value.However, due to jQuery's problematic error handling you won't notice this unless you return a promise from your callback, like you do in your example. It would be much more of a problem if you had compared
failto a standard-adheringcatchinvocation.