It is helpful to think of asynchronous data as only being defined inside the success callback. While it may not always be true, it's the only time when it's ALWAYS defined. Anytime that you are outside the success callback, the data may not have been retrieved yet and the callback may not have run yet.
This is what your program looks like if you remove the asynchronous call:
function example() {
var post = object.get("owner");
post.fetch();
console.log(window.titles);
}
It is more obvious here that window.titles is undefined.
On a separate thread, elsewhere, once post.fetch() is complete, this will be executed (at some time in the future, who knows when... it could be in 1 ms, it could take 1 day, you really cannot know):
(function(post) {
window.titles = post.get("username");
console.log(window.titles);
})()
Do you see now what the problem is? window.titles has not been set yet. post.fetch()
has been called, but the success callback has not returned yet.
There is no way to do what you're asking, which is, to retrieve data before you've actually retrieved data. Anything that requires the predictability of having fetched the data, must happen inside the callback (Edit: Or you may use promises or deferreds or etc.. but those are more complicated for what you are trying to do here). So that is why your console.log() inside the success callback works, and the one outside will probably never work.