I cannot get processed / failed / always callbacks for each of my ajax requests to execute before the deferred object callbacks.
The problem is that some of my ajax requests may fail, but I need to execute the same code if one of them fails or no one works.
Itβs hard to explain for sure, so I made this script to help show the problem I am having. http://jsfiddle.net/zZsxV/
var a1 = $.Deferred(); var a2 = $.Deferred(); var a3 = $.Deferred(); a1.done(function() { $('body').append('a1 done<br />'); }).fail(function() { $('body').append('a1 fail<br />'); }).always(function() { $('body').append('a1 always<br />'); }); a2.done(function() { $('body').append('a2 done<br />'); }).fail(function() { $('body').append('a2 fail<br />'); }).always(function() { $('body').append('a2 always<br />'); }); a3.done(function() { $('body').append('a3 done<br />'); }).fail(function() { $('body').append('a3 fail<br />'); }).always(function() { $('body').append('a3 always<br />'); }); var def = $.when(a1, a2, a3); def.always(function() { $('body').append('defer always <-- should be after all<br />'); }); setTimeout(function() { a1.resolve(); }, 5000); setTimeout(function() { a2.reject(); }, 1000); setTimeout(function() { a3.resolve(); }, 3000);
I read a lot of answers on this topic, but I do not believe that this exactly matches my need.
Any information needed to help, let me know and I will add it when I get back. Thanks in advance.
Edit
I understand what is happening. I just know how to do this to avoid this problem. I tried to use. The same thing with the same results. When one request is denied, it calls an error callback before waiting for other callbacks.
source share