Sorry guys and gals, can't seem to wrap my head around this one, despite the various scenarios I've read online. So here is the situation:
I've got an array of strings called recipientsList
For each string in that array, I want to make an AJAX call that passes the string in as a parameter, then returns the data from the AJAX to my callback function.
My code is below. What is happening is that if I have three strings in the recipientsList
array, then my expandDistributionList
function gets called 3 times, but the code never hits the success portion of the AJAX call until after my .each finishes iterating. Thoughts?
function expandDistributionList(emailAddress, cb) {
$.ajax({
url: "/expandDistributionList",
type: 'get',
dataType: 'json',
context: this,
data: {
distListName: emailAddress,
},
success: function(data) {
cb(data);
},
error: function() {
ace.search.mainframeErrorSignal = $.signal({
'message': 'Unable to expand distribution list.',
'addClass': 'error',
'sticky': true
});
}
});
}
recipientsList.each(function(item) {
expandDistributionList(item, function(data){
if(data...) {
//do some code here
}
else {
//do something else
}
});
});