Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 244
Umur Kontacı Avatar asked Dec 31 '12 07:12

Umur Kontacı


People also ask

Does promise all wait for all promises?

all() will reject immediately upon any of the input promises rejecting. In comparison, the promise returned by Promise. allSettled() will wait for all input promises to complete, regardless of whether or not one rejects.

How do you wait for promises to complete?

You can use the async/await syntax or call the . then() method on a promise to wait for it to resolve. Inside of functions marked with the async keyword, you can use await to wait for the promises to resolve before continuing to the next line of the function.

How do you check if all promises are resolved?

Checking if All Promises are Resolved Successfullyall() method can be used to check whether all Promises have fulfilled successfully. It accepts an iterable object (e.g. an array) of multiple Promises and returns a Promise. The returned Promise is resolved if all the input Promises passed to it are resolved.

What happens when a promise is resolved?

A Promise that is resolved with the given value, or the promise passed as value, if the value was a promise object. It may be either fulfilled or rejected — for example, resolving a rejected promise will still result in a rejected promise.


2 Answers

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.

like image 95
iwein Avatar answered Sep 18 '22 17:09

iwein


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?

like image 22
mcampster Avatar answered Sep 17 '22 17:09

mcampster