jQuery has, since version 1.5, utilities to handle management of callbacks and asynchronous invocations using objects called Deffered. Using these types of objects, it is easier for a client to add callbacks to be called when some background work has been completed. Here is an example using your code:
function some_func_validate(some_id) {
var deferred = $.Deferred(),
context = {
id: some_id,
success: false
};
$.ajax({
type: 'GET',
url: '/something/'+some_id+'/check'
})
.done(function(response){
context.success = true;
context.content = response;
deferred.resolveWith(context);
})
.fail(function() {
deferred.rejectWith(context)
});
return deferred.promise();
}
Example usage:
some_func_validate(5).then (
function (context) {
// Handle successful validation.
console.log(context);
},
function (context) {
// Handle failed validation.
console.log(context)
}
);
Another usage example:
function logger (context) {
console.log(context);
}
function onSuccessfulValidation (context) {
// Handle successful validation.
// context contains {id, content, success}
}
function onFailedValidation (context) {
// Handle failed validation.
// context contains {id, success}
}
some_func_validate(3).then (
[logger, onSuccessfulValidation],
[logger, onFailedValidation]
);
variable_to_returninto the success callback.$.ajaxis async. Please make your own research