I have an angular service/resource combo that does async call to server to get User Preferences. It is called UserPreferencesService and got a method load:
/// UserPreferencesService.load method
CommonService.asyncCall(UserPreferencesResource.getAll, {username: userName}).then(
function (data) {
userPreferenceData = data;
}
where asyncCall stands for trivial angular async server call that returns deferred.promise. It is called at some point.
Same UserPreferencesService also has method getCurrentProject() to return these preferences to controllers that are interested in, with calling code:
var myControllerInit = function()
{
var prj = UserPreferencesService.getCurrentProject();
}
I have a situation that some controller (that displays "current project" on screen) calls "getCurrentProject" WHILE "load" async call is in progress, so getCurrentProject does not return neeeded data, as these are not fully loaded yet.
How I can make it so that UserPreferencesService.getCurrentProject() will "wait" till the request resolves, if it is underway, and only then return the value? I suppose I can try to use promises, and I would like to have elegant, angular-style solution to the problem.