Sunday, 30 July 2017
javascript - jQuery Deferred not calling the resolve/done callbacks in order
Answer
Answer
Code example: http://jsfiddle.net/MhEPw/1/
I have two jQuery Deferred objects.
I want to have more than one 'async' request happening - and after they all run I want the callbacks (the .done functions) to be run in order they were specified in. Unfortunately they don't run in order.
Maybe I am looking for some functionality here that Deferred doesn't provide?
Answer
What you need to do is link all of your request with one master deferred object and register all of your callbacks on its promise. The master deferred object would need to listen to the individual requests and resolve accordingly. The simplest way to achieve this would be to define all of the deferred objects up front to avoid the chicken and egg problem:
var d1 = $.Deferred();
var d2 = $.Deferred();
var def = $.when(d1, d2);
def.done(function() {
alert(1);
});
setTimeout(function() {
d1.resolve();
}, 3000);
def.done(function() {
alert(2);
});
setTimeout(function() {
d2.resolve();
}, 1000);
Fiddle: http://jsfiddle.net/pVVad/
Changing the order of deferred objects definitions is possible but it would make the example much more complicated.
casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV
In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...
-
Parse error: syntax error, unexpected '(', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' ...
-
I'm using Simple HTML DOM to extract data from a HTML document, and I have a couple of issues that I need some help with. On the line th...
-
My ASP.NET WebForms app running on IIS 7.5 works fine when the request comes from the web server but throws the following error when the sam...
No comments:
Post a Comment