Organizational Research By

Surprising Reserch Topic

Question:Hold a service's initialization until all promises are resolved


In routeProvider we can hold the routing if we give a resolve object which contains promises; it would wait until all the promises are resolved. However, I couldn't find a way do it in initialization of the application.

There is angular.module("app", []).run(function (){ //init app }) but for a $resource or $http which is async, the app can finish initialization before the dependencies (promises) are resolved that would create a race condition. We don't want that.

So the question is, is there a way which would hold the initialization of a service until all the given promises are resolved?

 


asked Sep 13, 2013 in rauter by rajesh
edited Sep 12, 2013
0 votes
33 views



Related Hot Questions

2 Answers

0 votes
I've seen a similar problem. A somewhat elegant solution a team mate employed was a combination with RequireJS and it's domReady module:

define(['require', 'angular', 'app','routes', 'pendingServices'],
      function (require, ng, app, pendingServices) {

  /* place operations that need to initialize prior to app start here
   * using the `run` function on the top-level  module
   */
  app.run(pendingServices.init)

  require(['domReady!'], function (document) {
      /* everything is loaded...go! */
      ng.bootstrap(document, ['mainModule']);
  });

});

In the init method you can do all the preloading (and wait for the desired promises). I'm interested to hear other solutions of course.
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
0 votes
I've seen a similar problem. A somewhat elegant solution a team mate employed was a combination with RequireJS and it's domReady module:

define(['require', 'angular', 'app','routes', 'pendingServices'],
      function (require, ng, app, pendingServices) {

  /* place operations that need to initialize prior to app start here
   * using the `run` function on the top-level  module
   */
  app.run(pendingServices.init)

  require(['domReady!'], function (document) {
      /* everything is loaded...go! */
      ng.bootstrap(document, ['mainModule']);
  });

});

In the init method you can do all the preloading (and wait for the desired promises). I'm interested to hear other solutions of course.
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013

...