The operation is asyncronous, so you need to use some kind of callback or promise. Here's a basic callback example:
var getProfile = function( link, callback ){
profiles[ link.attr('profile-name') ] = {
//Some profile specific info
}
//Profile object becomes ready to return after some time
setTimeout(function(){
console.log('Returning profile object');
callback(profiles[link.attr('profile-name')]);
}, 400);
}
getProfile(link, function(result){
displayProfile(result);
});
The getProfile
function now takes a callback as the second argument, which is called when the timer completes, passing the profile name.