I have a function that is called if a certain id is going to be visible. This function calls a jQuery AJAX call that i have async set to false on. I get the deprecated warning in my console log so that tells me it's a sync call. I pass back the callback function but it's not waiting for my callback to finish before it runs the console log in the example below. Ideas as to why my function that calls the sync call is being ran asynchronously? I unfortunately can't use any ES6 or ES7 methods in my code.
if (fsReturned[0].id === 'the-id') {
// sync call with callback
keyCheck('key', function (resObj) {
console.log('precheck active key = ' + resObj.activeKey);
if (elResponse.activeKey === '1') {
fsReturned = $('#id');
} else {
fsReturned = $('#id').next();
}
});
console.log('This runs before my callback function finishes');
}
function sendSyncRequest(requestData, url, successFunction, authKey, method) {
header = {
"Authorization": "Bearer " + authKey,
"Content-Type": "application/json"
}
requestParameters = {
url: url,
method: method,
crossDomain: true,
headers: header,
async: false,
data: JSON.stringify(requestData)
}
$.ajax(requestParameters).then(
function (response, statusText, xhr) {
// successful calls
successFunction(response);
},
function (xhr) {
// api failures
successFunction(null);
});
}
function keyCheck(elValue, callback) {
var url = 'url',
stageKey = 'key',
requestData = {
activity: 'something',
el: elValue
};
successFunction = function (response) {
// processed response and created elResObj object
var processed = JSON.parse(response);
var elResObj = {
activeKey: processed.activeKey,
};
return callback(elResObj);
};
sendSyncRequest(requestData, url, successFunction, stageKey, 'POST');
};
I need to update a global variable "fsReturned" with the data I get from the sync call before proceeding with the rest of the code.
Any assistance is greatly appreciated. Thanks!