Just move the console.log()
into the "done" callback function because you can't know exactly when or if it will complete. This is the point of the "done" callback function - - to defer work that needs to be done after a successful call has been made.
JavaScript promises (or JQuery deferreds) could also be used to disconnect the callback(s) from the initial AJAX request for more flexibility, but the nature of AJAX stays the same.
$.get("./whazzup.txt", function(text) {
// This function will only be executed when the AJAX call has successfully completed
msg = text;
console.log(msg);
});
// This one was working, because you are basically telling the JavaScript
// runtime to wait 3 seconds (at least) before writing the message and by
// that time the AJAX call has completed. But, this technique is not
// reliable because there may be times when the AJAX call hasn't completed
// after 3 seconds. You just can't know how long it will take.
setTimeout(function(){ console.log(msg); }, 3000);