javascript - How to wait for async angularJS call to complete if concurrent call requested the data being still loaded? -


I have an angular service / resource combo that gives the user an asynchronous call to get the user priority. It is called UserPreferencesService and receives a method load:

  /// User Provider Service. Load method CommonService.asyncCall (UserPreferencesResource.getAll, {username: user name}). (Function (data) {userPreferenceData = data;}   

Where asyncCall means the angular ASINC server call which is postponed, which is postponed. Promoter. It is called at some point.

Function () {var prj = UserPreferencesService.getCurrentProject ();}

I have one such The situation is that some controller (which shows the "current project" on the screen) call "getCurrentProject" WHILE "load" async call is in progress, so getCurrentProject does not return nested data, because it is not fully loaded yet.

How can I make it so that the user can wait "wait" through service provider service if it is running, and only returns the price, I think I use the promises I can try to do this, and I have to solve the elegant, angular style for this problem.

Disclaimer : I am not familiar with Kangaroo and the way it promises And handles asynchronities.


In Javascript, you can not block a promise until it is resolved; You can only make a promise that depends on the promise, returns your promise, in other words tied to the original one: You can not do the synchronous code which depends on the async code.

If I understand correctly, no action should be postponed to the priority service unless it is loaded. You can not synchronize it, but you can return a promise that depends on its asynchronous loading in each of your other ways.

If this is what you want, then you should protect the methods of each service with the original loading promise with .then . This promise should be solved once, when the service is loaded. Further inventions 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;}); Returned talk; }; This.getCurrentProject = function () {return.load (). Then (function () {return doSomethingWith (userPreferenceData);}); }}    

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -