The code you have posted is not synchronous, so it is working correctly.
This is the flow of your code to give you a better understanding of what the code is actually doing.
// myVar is declared undefined
var myVar;
// you call console.log with undefined
console.log(myVar);
// sometime later .then is called
myModule.myFunction().then(function(data){
// later you set the global variable to the data
myVar = data;
// at this point you can log the value of myVar
});
In terms of returning something from an ajax call you can't do it synchronously, but you can chain another .then
after it's returned.
myModule.myFunction().then(function(data){
// do something with the data
return data;
})
.then(function(data){
// do something else with the data
})
You can keep chaining then
as long as you want, and there are more useful methods you can use for error handling, or calling promises in parallel or synchronously