This is impossible. $.Ajax calls will always return immediately. You need to deal with the return when it is called through a callback (possibly several seconds later). Javascript never blocks for a given call. It may help to think of your code like this:
//This entirely unrelated function will get called when the Ajax request completes
var whenItsDone = function(data) {
console.log("Got data " + data); //use the data to manipulate the page or other variables
return data; //the return here won't be utilized
}
function myFunction(){
$.post(postURL, mydata, whenItsDone);
}
If you’re interested more on the benefits (and drawbacks) of Javascript’s no-blocking, only callbacks: this Node.js presentation discusses its merits in excruciating detail.
solved JQuery $.post in a function. Wait for callback to define the return. [duplicate]