I have a comma separated list of names. I want me to want to invoke a server request for all names in a sequence and store the result inside an array. I tried and it works when I have the number of names that are on the line.
See Here - It Works When I Know the Number of Names
Now I want this code to be shared. If I add one name to this line, it should process automatically without adding any code for the ajax request.
See Here - This is what I tried. It does not work as expected.
shoppingList = shoppingList.split(","); var result = []; function fetchData(shoppingItem) { var s1 = $.ajax('/items/'+shoppingItem); s1.then(function(res) { result.push(new Item(res.label,res.price)); console.log("works fine"); }); if(shoppingList.length == 0) { completeCallback(result); } else { fetchData(shoppingList.splice(0,1)[0]); } } fetchData(shoppingList.splice(0,1)[0]);
Problem
I do not understand how to determine that all promise objects have been resolved so that I can call the callback function.
javascript ajax recursion
Jay shukla
source share