I'm trying to use a .when with an .each to loop through some elements and .then do something with the results. But I'm not sure how to store the results from the .when to use in the .then.
for example
var widget_data = [];
$.when(
$.each(widget_ids, function(index, widget_id){
$.get('/widget/' + widget_id, function(data){
widget_data.push(data);
});
})
)
.then(function(){
console.log(widget_data);
});
The widget_data array is empty after running.
console.log(data)inside$.getand see whether you are gettingdatathere.whenworks and shows the resulting data. it seems.thenis run right away without waiting for the.whento finish..doneinstead of.then