javascript - Listen for multiple promises to complete -


this works fine the first call, regardless on if "reset" deferred objects, $.when trigger once, unless rebind inside click handler.

is there way make global listener several promises, can resetted?

i've thought custom events , pub/sub pattern well. work great single events, can't figure out way use them scenario below.

var dfd1 = $.deferred(), dfd2 = $.deferred();  $('input').on('click', function(){     dfd1 = $.deferred();     dfd2 = $.deferred();     f1();     f2(); });  $.when.apply($, [dfd1, dfd2]).then(function(schemas) {     console.log('done'); });  function f1(){     settimeout(dfd1.resolve, 500); }  function f2(){     settimeout(dfd2.resolve, 600); }  f1(); f2(); 

http://jsfiddle.net/c62gh/

tl;dr: have call $.when again every time create new deferreds.

the problem code don't "reset" deferred objects (and in fact cannot reset) -- replacing references these objects references other (unresolved) deferreds.

note said some references because references replacing not unique: array passed $.when holds separate references same deferreds, , references have of course not been replaced. if had, $.when has no way know if wanted to.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -