Skip to content

Allow Asynchronous APP_INITIALIAZER #5929

Closed

Description

@jteplitz

Sometimes you may want to have an async APP_INITIALIZER that needs to be resolved before bootstrapping. For example in a WebWorker you may want to receive some values from the UI (such as the browser location or history) before the app bootstraps. This is distinct from bootstrapping with asyncApplication because the DI providers are not themselves asynchronous, but rather some async work must be done after the providers are set up.

CC @vsavkin

Activity

  1. vsavkin commented on Dec 16, 2015

    @vsavkin
    Contributor

    Option 1

    We can support it as follows:

    function funcReturningAPromise:Promise() {...}
    
    platform(MY_PLATFORM).
      asyncApplication(
        () => [], 
        [provide(APP_INITIALIZER, {useFactory: () => funcReturningAPromise})]).
      then(appRef => ...);
    
    • Angular will check the results of app initializers, collect all the promises, and wait for them to be resolved before resolving the application ref promise.
    • Angular will throw if an initializer returned a promise when application was used instead of asyncApplication.

    Option 2

    Another option would be to do the following:

    function funcReturningAPromise:Promise() {...}
    
    let appRef = platform(MY_PLATFORM).application([platform(APP_INITIALIZER, {useFactory: () => funcReturningAPromise})]);
    appRef.get(APP_INITIALIZERS_COMPLETED).then(_ => {
    
    });
    

    This would work with both application and asyncApplication.

    I think Option 1 is cleaner.

  2. jteplitz commented on Dec 16, 2015

    @jteplitz
    ContributorAuthor

    I also think Option 1 is cleaner. I'm happy to go ahead and implement it if you want. I think if we do option 1 we should allow people to pass in null as the first argument to asyncApplication as well as a function.

  3. 8 remaining items

  4. added 5 commits that reference this issue on Jan 13, 2016
    8b186fa
  5. angular-automatic-lock-bot commented on Sep 7, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  6. locked and limited conversation to collaborators on Sep 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions