After coming across a helpful answer in the question titled How do I return the response from an asynchronous call?, I attempted to implement it without success.
Reviewing Hemant Bavle's answer (currently with 62 votes) gave me hope, but my implementation is still not working as expected. Below is my code snippet (excluding ajaxSetup()
and fail()
for brevity):
function isGoodPIN(pin) {
var result;
var cURL = "server/checkPIN?pin=" + pin;
function setResult(ajaxResult) {
result = ajaxResult; // <--------- true here...
}
var ajaxResponse = $.get(cURL, function (data) {
// data is "OK" here...
setResult(data == "OK" ? true : false);
});
return result; //<--------- undefined here
}
I suspect that there might be a scope issue since the variable result in setResult()
is confined within the function. How can this problem be resolved?