javascript - $.Deferred: How to detect when every promise has been executed -
i have number of async tasks need completed, i'm using promises.
i need observe when each 1 of promises has been executed (both resolved , rejected). must not go on execution until point.
i using this:
$.when(promise1, promise2, ...).always(); but code wrong, because when method has lazy evaluation, , returns 1 of promises fails. always callback runs 1 of promises fail.
i thinking in coding workaround, utilize case mutual maybe has done already, or maybe there's way of doing using jquery (if not, nice add together promise.whennonlazy or promise.when(promise1, promise2, ..., false) in future.
is possible?
more sophisticated promise libraries have allsettled() function q or promise.settle bluebird.
in jquery, implement such function , extend $ namespace it, necessary if need , performance-optimized.
a simpler solution create new promise each of ones waiting for, , fulfilling them when underlying 1 rejected. can utilize $.when() on them without problems. in short:
// using underscore's .invoke() method: $.when.apply(null, _.invoke(promises, "then", null, $.when)).done(…) more stable:
$.when.apply($, $.map(promises, function(p) { homecoming p.then(null, function() { homecoming $.deferred().resolvewith(this, arguments); }); })).done(…); you might alter then callbacks bit distinguish between fulfilled , rejected results in final done.
javascript jquery asynchronous promise jquery-deferred
No comments:
Post a Comment