Since .post runs async your method will always return true at the moment. But how about returning a deferred object instead? I realize that this is a different approach than the one you're asking for, and it might not be applicable for your scenario.
Something like this (from the top of my head):
function test(){
var def = new $.Deferred();
$.ajax({
url: 'test',
success: function(data) {
// Do some minor logic to determine if server response was success.
console.log('success in callback');
if(data == true){
def.resolve(true);
}
else{
def.reject();
// or possibly def.resolve(true) depending what you're looking for.
}
},
error:function(){
console.log('error in callback');
def.reject();
}
});
return def;
}
function testMethod(){
var defObject = test();
defObject.done(function(result){
console.log('done in deferred');
// Do something when everything went well.
}).fail(function(){
console.log('error in deferred');
// Do something when something went bad.
});
}
$(document).ready(function(){
testMethod();
});
Deferred makes it easy to listen async callbacks. It's a common pattern when you want to do some logic efter a request is done, but you want to separate the logic from the request itself.
Some more examples here:
http://blog.mediumequalsmessage.com/promise-deferred-objects-in-javascript-pt2-practical-use
A plunkr example:
http://plnkr.co/edit/LsNjfx4alNzshOQb2XAW?p=preview