my issue appears similar to this one but I haven't been able to resolve it with those answers.
In short, at page load an ajax call generates a dropdown select element, then its default value is set depending on some parameters.
The code goes something like this:
getList('country', null, q, 'category:customer', null);
newReportSelection.apply($("#report"));
function getList(field, val, q, fq, default_value) {
$.get(... {
// call info
}, function(result) {
// appending the dropdown to DOM
});
};
newReportSelection = function () {
// determine and select default option
}
getList is used at various places so I can't add this (specific to page load) newReportSelection() code to the ajax callback function.
Putting a promise around the getList() call doesn't wait for the Ajax call since it's asynchronous.
Putting one around the Ajax call waits for it to be sent, but not for the callback function to be executed.
Putting one around the callback function doesn't work either, as the Ajax part waits for it but getList() kept going because of the asynchronicity.
Even if chaining promises on the Ajax call and the callback function worked, that'd look like a pretty dirty workaround and undesirable.
Is there a way for me to make the newReportSelection() call wait until the whole getList() is done, without manually setting the Ajax calls to async: false
?
Thanks in advance.