Consider a scenario where there is a JavaScript function that returns a boolean value:
function UpdateUserInSession(target, user) {
var data = { "jsonUser": JSON.stringify(user) };
$.ajax({
type: "POST",
url: target,
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
return true;
},
error: function (xhr, status, error) {
return false;
}
});
}
The server being called might take approximately 15 seconds to respond. How can we ensure that the function does not exit before the server call completes? How do we guarantee that the caller receives a proper response (true/false) instead of undefined?
PLEASE NOTE: Using async: false may cause the UI to become unresponsive, so it's not preferred.