I encountered an issue with my code where an ajax call, referred to as callback (b), was expected to depend on the success of another ajax call, known as callback (a). Surprisingly, callback (b) completed successfully before its parent ajax call (a) finished.
Here is the Javascript code:
var ajaxAdata; //global
ajaxA(ajaxB(1));
function ajaxA(callback){
FB.api('/me', function(response) { //ajax call(a)
ajaxAdata = response.id;
callback(); // this completes before ajax call(a) completes
}
}
ajaxB = function(isPublic) {
.getJSON(){ //ajax call (b)
console.log(ajaxAdata); // necessary ajaxAdata returns undefined
}
}
Is there a misunderstanding on my part when it comes to JavaScript? I have learned that using a callback function is the correct approach for handling asynchronous calls. However, does JavaScript preemptively execute the ajaxB function and initiate .getJSON() even before the FB.api() call has concluded?