I have the following JavaScript (and jQuery) code:
function checkEmail(email) {
if (email.length) {
$.getJSON('ajax/validate', {email: email}, function(data){
if (data == false) {
// stuff
}
return data;
})
}
}
I want the anonymous function to return data
to the parent function, checkEmail()
. I tried doing something like this:
function checkEmail(email) {
if (email.length) {
var ret = null;
$.getJSON('ajax/validate', {email: email}, function(data){
if (data == false) {
// stuff
}
ret = data;
})
return ret;
}
}
But of course this won't work because the $.getJSON()
call is asynchronous, so it will return ret
before the GET request is finished.
Any thoughts here?
Thank you!