Differences between Deferred.then (null, func) and Deferred.fail (func) in jQuery?

Today I found out that Deferred.then(null,func) and Deferred.fail(func) not the same in jQuery. In the ES6 promise, Promise.then(null,func) and Promise.catch(func) are the same thing, so I was confused by the jQuery functions.

The only difference I know of is:

 $.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?

0
source share
1 answer

Yes, the difference between the two is that .fail() returns the original promise it was called on, while .then() creates a new promise that can be resolved with a different value.

However, due to problematic jQuery error handling, you will not notice this unless you return the promise from your callback, as in your example. This would be much more problematic if you compared fail with the standard catch technique.

0
source

Source: https://habr.com/ru/post/988704/


All Articles