最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Hold a service's initialization until all promises are resolved - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Mar 1, 2013 at 12:16 Umur Kontacı asked Dec 31, 2012 at 7:35 Umur KontacıUmur Kontacı 35.5k8 gold badges74 silver badges96 bronze badges 10
  • 1 Don't you want Manual Initialization? – Jared Farrish Commented Dec 31, 2012 at 8:02
  • 1 Setting a callback to manual init of a promise resolve would work, however it is not elegant, angular has a great DI, why can't we benefit from that? Like if the dependency is a promise, resolve it before going further – Umur Kontacı Commented Dec 31, 2012 at 8:20
  • Since you didn't include yourj apps actual code, I can't tell but I think this questions is covering the same issue: stackoverflow./questions/12265565/… – Jared Farrish Commented Dec 31, 2012 at 9:41
  • No, I want to hold the init of a service until the promises are resolved, this is a generic question, does not need a source. – Umur Kontacı Commented Dec 31, 2012 at 9:53
  • 1 If you run the $timer from within the run, it'll maintain it's state through .call(), so I would think that's probably the most elegant. Wait for the promise to signal to run() they're resolved and it cancels the $timer. The docs seem to make that sound the way they intended it to work. – Jared Farrish Commented Dec 31, 2012 at 14:07
 |  Show 5 more ments

2 Answers 2

Reset to default 5

I've seen a similar problem. A somewhat elegant solution a team mate employed was a bination 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.

Just thinking out load here, but how about only declaring 1 'catch all' route to begin, and in that route provider, hold the loading of the route until you have done everything you need. (using resolve and promises).

Then, when you're done, register the remaining routes, and reload the original route. This time, a more specific handler should be registered and it will bypass your 'catch all' initializer.

What do you think, any issues?

发布评论

评论列表(0)

  1. 暂无评论