You seem to need to make the ajax call synchronous. You can do that like this:
$.ajax({
...
async: false,
success: ...
});
return a;
This way the JS execution will pause until the call returns and the success
function runs.
Of course there is the issue of sync calls. It's best if you refactor your code so that you do what you need to do with the a
variable in the success
callback.
Building on this idea, suppose your f3
function was something like this:
var f3 = function() {
a = f1(arg);
alert(a); //i.e. "do something" with "a"
}
You could do this instead:
var f3 = function() {
f1(arg);
}
var f3_callback = function(a) {
alert(a); //i.e. "do something" with "a"
}
So, your success function would look like this:
success: function(data) {
a = f2(data);
f3_callback(a);
}
I hope this is clear!