I am trying to permanently change some values from inside the $.ajax.done() scope, so that it is available to other parts of my code outside this scope. The variable duplicate is a global variable within a module I have written and I want to be able to use the updated boolean value in other functions after running done().
The function duplicateFile is set up to return a block of $.ajax({ url:... }); code.
// Callback function
duplicateError = function() {
console.log(duplicate); // prints 'true' if a duplicate is found
if (duplicate) {
errors.create("duplicateFile");
}
};
// AJAX call
duplicateFile(fileData).done(duplicateError);
If a duplicate is found after running the AJAX call, console.log prints true. But if I try to call console.log after the AJAX function call as follows, the value is false regardless:
duplicateFile(fileData).done(duplicateError);
console.log(duplicate); // always prints 'false'
Is there any way to work around this and get the duplicate value to change permanently after calling the request? I know I could set async: false in the AJAX request, but I would like to avoid that.
EDIT: duplicate is getting changed in the callback function that is passed to the success method in the$.ajax({ url:... }) .