'How to Force a Function to Wait Until a bootbox Confirmation Result Arrives?

Following script reload the page before uploading all the files. I can manage to upload all the files if there is no duplicate. But not with bootbox confirmation box.

<script language="javascript">
        //This function is called on drop event to upload multiple files
        function file_uploads(files_obj, tocType) {
            if(files_obj != undefined) {
                var fileArr = [];
                for(i=0; i<files_obj.length; i++) {
                    var FName = files_obj[i].name.toUpperCase();
                    //validate_duplicate_file function make an ajax call to see if file already exist in Database
                    var IsDuplicate = validate_duplicate_file(files_obj[i], tocType);
                    if(IsDuplicate > 0){
                        //If file already exist it will ask for overwrite confirmation 
                        bootbox.confirm({
                            message: "<h3>Please Confirm...</h3><br> <p>Are you sure you want to overwrite <b>" + FName + " </b> ?</p>",
                            buttons: {
                                cancel: {label: 'No', className: 'btnBlack' },
                                confirm: {label: 'Yes', className: 'btnBlack'}
                                },
                            callback: function (result) {
                                if (result) {
                                    //If user allow to overwrite then upload the file to Database
                                    ajax_file_upload(files_obj[i])
                                }
                            }
                        });
                    }else{
                        //If not duplicate then upload the file to Database
                        ajax_file_upload(files_obj[i])
                    }
                }
                //Once all the files are uploaded reload the locations
                location.reload();
            }
        }
    </script>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source