I have a dynamic number of .get requests that I am using .when to wait until they complete so that I can work with all of their responses
// function that returns the response of a get request
function getRecipe(recipeID){
var url2 = 'https://api.pearson.com/kitchen-manager/v1/recipes/'+recipeID;
return $.get(url2);
}
// function that loops through an unknown sized array and calls the getRecipe function for each one and adds the request to a requests array
function getAllRecipes(recipes_array){
var requests = [];
for (var i = 0; i < recipes_array.length; i++) {
var recipeID = recipes_array[i];
requests.push(getRecipe(recipeID));
}
// this is where I would like to wait for all of the requests to come back so I am trying to use $.when
}
Without a dynamic number of requests I would structure it something like this
$.when( d1, d2, d3 ).done(function ( v1, v2, v3 ) {
// v1, v2, and v3 are the return responses from each request
});
And v1, v3, and v3 should be the returned value from each request
I have tried the following with no luck...
$.when(requests).done(function(stuff) {
// stuff returns an array of the correct number of objects but not the returned values, just objects
// $.type(stuff[0]) == object
}
As well as ...
$.when.apply($, requests).then(function(stuff) {
// stuff returning 3 items in an array
// item 1 is the response from the last request
// item 2 is a string "success"
// item 3 is an object
}
How can I access all of the requests responses when they are dynamic?
I have referenced the following to get this far: