Disclaimer: I'm not familiar with Angular and the way it handles promises and asynchronicity.
In JavaScript, you can't block until a promise is resolved; you can only make code that relies on a promise return its own promise, chained to the original one. In other words: you can't have synchronous code that relies on async code.
If I understand correctly, you want to defer any action on the PreferencesService until it is loaded. You can't do this synchronously, but you can make each of its other methods return a promise that depends on its async loading.
If this is what you want, you should guard each of the service's methods with .then
on the original loading promise. This promise should be resolved once, when the service is loaded. Further invocations of other methods will be resolved immediately.
Example:
function UserPreferenceService() {
var loadedPromise = null;
var userPreferenceData = null;
this.load = function() {
if (loadedPromise == null)
loadedPromise = CommonService.asyncCall(UserPreferencesResource.getAll, {userName: userName})
.then(function(data) { userPreferenceData = data; });
return loadedPromise;
};
this.getCurrentProject = function() {
return this.load().then(function() {
return doSomethingWith(userPreferenceData);
});
}
}