I have a jquery method:
$.when(
function() {
console.log("1st Func!");
},
function() {
console.log("2nd Func!");
}
).done(function() {
console.log("Done Func!");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
And I expect the first two method call and finished and at the end the done method run. I expect in console:
1st Func!
2nd Func!
Done Func!
But only the done method called and the console is like: Done Func!
Where is the problem?
UPDATE:
I get from the comments the function inside the When should be a promise. In actual sample I have:
var getAndSetFromAPI = function(keyName, callback){
if (localStorage.getItem(keyName) === null){
$.AjaxCall(.., .., callback(data));
}
else{
if(callback){ callback(localStorage.getItem(keyName))}
}
};
And I have a list of objects:
mylist.each(function (index, element){
getAndSetFromAPI(element, function(date){fillData(element, data);}
});
So What is your suggestion to wait to complete each element and then go to process another one?