Simplest way to combine jQuery promises? -
Simplest way to combine jQuery promises? -
i have many situations need accumulate multiple jquery promises (animation or ajax or "other"), simple test tried did not wait of them finish (or of them):
jsfiddle: http://jsfiddle.net/trueblueaussie/j7el4/2/
function makepromises() { $('#box1').animate({ left: 300 }, 4000); $('#box2').animate({ left: 300 }, 1000); $('#box3').animate({ left: 300 }, 2000); $('#box4').animate({ left: 300 }, 3000); homecoming $('.box').promise(); }; $.when.apply($, makepromises()).then(function(){ $('.box').addclass("done"); }); worst case, have expected wait first match before making boxes grey.
why not working expected?
update:now know jquery correctly combines promises multiple elements, note can utilize empty jquery object's promise provide empty promise
e.g.
return $().promise(); this great homecoming value (e.g. when no animations started @ all).
why not working expected?
because apply accepts array sec argument, you're passing promise.
since $(".box").promise() returns promise resolved when queued actions done, , isn't array, can't utilize apply that.
all need utilize result directly:
makepromises().then(/*...*/); updated fiddle
(a. wolff's code:
$.when(makepromises()).then(/*...*/); ...would work, unnecessary indirection unless have other promises need combine with.)
jquery promise
Comments
Post a Comment