I'm struggling to grasp how I can effectively wait for the results of multiple functions that involve ajax requests.
I experimented with using Promise.all()
and $.when().done();
, but found them complicating my code rather than simplifying it. I also want to steer clear of using async: false
within the ajax calls.
The structure of my main function doesn't necessarily have to be like that. I simply need a method that allows me to call one or more functions containing ajax requests, then pause the execution until all results are received without branching off into other functions.
function main(){
//some functions
UploadFile('input_1');
UploadFile('input_2');
.
.
.
UploadFile('input_n');
//Here is where I need to consolidate the results from the UploadFile functions
//some other functions
//return true or false based on //some functions, UploadFile AND //some other functions
}
function UploadFile(inputId){
return $.ajax({
//ajax parameters
success: function(IDUpload) {
if (IDUpload > 0) {
return true;
}
return false;
},
error: function(error) {
return false;
}
});
}
Edit:
The main()
function serves as the form validation function. It appears that setting it as async might not trigger at all; it won't wait for the UploadFile calls.