jquery - How to transform an array of promise objects into a promise object of an array? -
is there way transform array of promise objects promise object of array? i'm looking behaves kind of $q.all(promises)
in angularjs.
here's code:
var getpages = function(courses) { var courses_promises = courses.filter(function(item) { return item.courses[0].home_link; }).map(function(item) { deferred = $.deferred(); item["class_link"] = item.courses[0].home_link; item["home_link"] = item["class_link"] + "class/index"; $.get(item.home_link) .then(function(response) { item["html"] = response; deferred.resolve(item); }); return deferred.promise(); }); return $.when.apply($, courses_promises); };
i want getpages
function return single promise resolved array of values, each value corresponding promise @ same index in promises array. if of promises resolved rejection, resulting promise resolved same rejection.
and use
getpages.then(getevents)
where getevents
function expects array elements resolved getpages
.
can me out? thank you!
if you're using q do:
q.all(promises)
that's better idea using jquery promises q has better design. in addition, safe give q.all
jquery promises , assimilat them fine.
if don't want use q, believe $.when
similar, it's not clean , simple.