Chaining APP_INITIALIZER functions in Angular 6











up vote
0
down vote

favorite












I am trying to ensure that two items of config are loaded before my application starts while using Angular 6 and Typescript. I would like to retrieve an API endpoint from a config file, and then use that API endpoint to retrieve some permissions. The permissions then determine what should be shown on the screen, hence why I really want them to be loaded before the application.



I've read several posts about this including APP_INITALIZER won't delay initialization, Runtime configuration and Nested HTTP Requests
. However they all seem to require there to be one config.service.ts that handles all the config loading that is required. Is it not better practice to have one service to load the API endpoint (and other pieces of config), and another service to access the API?



I have put together a Plunker that demonstrates the two separate services to retrieve the information. The console is logging the various steps. The current output is:



going to load app settings
getting permissions
undefined (This is the api endpoint retrieved from the config file)
Angular is running in the development mode. Call enableProdMode() to enable the production mode.
got appsettings


I hope you can see that the code is trying to load the endpoint and trying to use it to retrieve the permissions simultaneously. The endpoint is actually retrieved after the application is loaded. I would like to have the api endpoint retrieved, THEN the permissions loaded and then the application loads. So the desired output is:



going to load app settings
got appsettings
getting permissions
https://webapi.endpoint/
Angular is running in the development mode. Call enableProdMode() to enable the production mode.


I'm new to Angular, so any help or pointers would be appreciated.



EDIT:
I've found this post but I don't understand the solution. It seems very related if anyone can help me unpick it?










share|improve this question




























    up vote
    0
    down vote

    favorite












    I am trying to ensure that two items of config are loaded before my application starts while using Angular 6 and Typescript. I would like to retrieve an API endpoint from a config file, and then use that API endpoint to retrieve some permissions. The permissions then determine what should be shown on the screen, hence why I really want them to be loaded before the application.



    I've read several posts about this including APP_INITALIZER won't delay initialization, Runtime configuration and Nested HTTP Requests
    . However they all seem to require there to be one config.service.ts that handles all the config loading that is required. Is it not better practice to have one service to load the API endpoint (and other pieces of config), and another service to access the API?



    I have put together a Plunker that demonstrates the two separate services to retrieve the information. The console is logging the various steps. The current output is:



    going to load app settings
    getting permissions
    undefined (This is the api endpoint retrieved from the config file)
    Angular is running in the development mode. Call enableProdMode() to enable the production mode.
    got appsettings


    I hope you can see that the code is trying to load the endpoint and trying to use it to retrieve the permissions simultaneously. The endpoint is actually retrieved after the application is loaded. I would like to have the api endpoint retrieved, THEN the permissions loaded and then the application loads. So the desired output is:



    going to load app settings
    got appsettings
    getting permissions
    https://webapi.endpoint/
    Angular is running in the development mode. Call enableProdMode() to enable the production mode.


    I'm new to Angular, so any help or pointers would be appreciated.



    EDIT:
    I've found this post but I don't understand the solution. It seems very related if anyone can help me unpick it?










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am trying to ensure that two items of config are loaded before my application starts while using Angular 6 and Typescript. I would like to retrieve an API endpoint from a config file, and then use that API endpoint to retrieve some permissions. The permissions then determine what should be shown on the screen, hence why I really want them to be loaded before the application.



      I've read several posts about this including APP_INITALIZER won't delay initialization, Runtime configuration and Nested HTTP Requests
      . However they all seem to require there to be one config.service.ts that handles all the config loading that is required. Is it not better practice to have one service to load the API endpoint (and other pieces of config), and another service to access the API?



      I have put together a Plunker that demonstrates the two separate services to retrieve the information. The console is logging the various steps. The current output is:



      going to load app settings
      getting permissions
      undefined (This is the api endpoint retrieved from the config file)
      Angular is running in the development mode. Call enableProdMode() to enable the production mode.
      got appsettings


      I hope you can see that the code is trying to load the endpoint and trying to use it to retrieve the permissions simultaneously. The endpoint is actually retrieved after the application is loaded. I would like to have the api endpoint retrieved, THEN the permissions loaded and then the application loads. So the desired output is:



      going to load app settings
      got appsettings
      getting permissions
      https://webapi.endpoint/
      Angular is running in the development mode. Call enableProdMode() to enable the production mode.


      I'm new to Angular, so any help or pointers would be appreciated.



      EDIT:
      I've found this post but I don't understand the solution. It seems very related if anyone can help me unpick it?










      share|improve this question















      I am trying to ensure that two items of config are loaded before my application starts while using Angular 6 and Typescript. I would like to retrieve an API endpoint from a config file, and then use that API endpoint to retrieve some permissions. The permissions then determine what should be shown on the screen, hence why I really want them to be loaded before the application.



      I've read several posts about this including APP_INITALIZER won't delay initialization, Runtime configuration and Nested HTTP Requests
      . However they all seem to require there to be one config.service.ts that handles all the config loading that is required. Is it not better practice to have one service to load the API endpoint (and other pieces of config), and another service to access the API?



      I have put together a Plunker that demonstrates the two separate services to retrieve the information. The console is logging the various steps. The current output is:



      going to load app settings
      getting permissions
      undefined (This is the api endpoint retrieved from the config file)
      Angular is running in the development mode. Call enableProdMode() to enable the production mode.
      got appsettings


      I hope you can see that the code is trying to load the endpoint and trying to use it to retrieve the permissions simultaneously. The endpoint is actually retrieved after the application is loaded. I would like to have the api endpoint retrieved, THEN the permissions loaded and then the application loads. So the desired output is:



      going to load app settings
      got appsettings
      getting permissions
      https://webapi.endpoint/
      Angular is running in the development mode. Call enableProdMode() to enable the production mode.


      I'm new to Angular, so any help or pointers would be appreciated.



      EDIT:
      I've found this post but I don't understand the solution. It seems very related if anyone can help me unpick it?







      angular rxjs






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 8:17

























      asked Nov 20 at 9:13









      Fletch

      146




      146
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          In the end I took inspiration from hackernoon and rearranged the way I called my promises. I kept my separate services, but called the permission service from the appsettings service.



          app-settings.service.ts:



          loadAppSettings() {

          return (): Promise<any> => {
          return new Promise((resolve, reject) => {
          console.log('loadAppSettings:: inside promise');

          setTimeout(() => {
          console.log('loadAppSettings:: inside setTimeout');
          this.permissionService.loadPermissions().then(x=> resolve());
          }, 5000);

          })
          }


          permissions.service.ts:



          loadPermissions() {
          return new Promise((resolve, reject) => {
          console.log(`PermissionsService:: inside promise`);

          setTimeout(() => {
          console.log(`PermissionsService:: inside setTimeout`);
          resolve();
          }, 3000);
          });
          }





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389661%2fchaining-app-initializer-functions-in-angular-6%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            In the end I took inspiration from hackernoon and rearranged the way I called my promises. I kept my separate services, but called the permission service from the appsettings service.



            app-settings.service.ts:



            loadAppSettings() {

            return (): Promise<any> => {
            return new Promise((resolve, reject) => {
            console.log('loadAppSettings:: inside promise');

            setTimeout(() => {
            console.log('loadAppSettings:: inside setTimeout');
            this.permissionService.loadPermissions().then(x=> resolve());
            }, 5000);

            })
            }


            permissions.service.ts:



            loadPermissions() {
            return new Promise((resolve, reject) => {
            console.log(`PermissionsService:: inside promise`);

            setTimeout(() => {
            console.log(`PermissionsService:: inside setTimeout`);
            resolve();
            }, 3000);
            });
            }





            share|improve this answer

























              up vote
              0
              down vote



              accepted










              In the end I took inspiration from hackernoon and rearranged the way I called my promises. I kept my separate services, but called the permission service from the appsettings service.



              app-settings.service.ts:



              loadAppSettings() {

              return (): Promise<any> => {
              return new Promise((resolve, reject) => {
              console.log('loadAppSettings:: inside promise');

              setTimeout(() => {
              console.log('loadAppSettings:: inside setTimeout');
              this.permissionService.loadPermissions().then(x=> resolve());
              }, 5000);

              })
              }


              permissions.service.ts:



              loadPermissions() {
              return new Promise((resolve, reject) => {
              console.log(`PermissionsService:: inside promise`);

              setTimeout(() => {
              console.log(`PermissionsService:: inside setTimeout`);
              resolve();
              }, 3000);
              });
              }





              share|improve this answer























                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                In the end I took inspiration from hackernoon and rearranged the way I called my promises. I kept my separate services, but called the permission service from the appsettings service.



                app-settings.service.ts:



                loadAppSettings() {

                return (): Promise<any> => {
                return new Promise((resolve, reject) => {
                console.log('loadAppSettings:: inside promise');

                setTimeout(() => {
                console.log('loadAppSettings:: inside setTimeout');
                this.permissionService.loadPermissions().then(x=> resolve());
                }, 5000);

                })
                }


                permissions.service.ts:



                loadPermissions() {
                return new Promise((resolve, reject) => {
                console.log(`PermissionsService:: inside promise`);

                setTimeout(() => {
                console.log(`PermissionsService:: inside setTimeout`);
                resolve();
                }, 3000);
                });
                }





                share|improve this answer












                In the end I took inspiration from hackernoon and rearranged the way I called my promises. I kept my separate services, but called the permission service from the appsettings service.



                app-settings.service.ts:



                loadAppSettings() {

                return (): Promise<any> => {
                return new Promise((resolve, reject) => {
                console.log('loadAppSettings:: inside promise');

                setTimeout(() => {
                console.log('loadAppSettings:: inside setTimeout');
                this.permissionService.loadPermissions().then(x=> resolve());
                }, 5000);

                })
                }


                permissions.service.ts:



                loadPermissions() {
                return new Promise((resolve, reject) => {
                console.log(`PermissionsService:: inside promise`);

                setTimeout(() => {
                console.log(`PermissionsService:: inside setTimeout`);
                resolve();
                }, 3000);
                });
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 at 10:02









                Fletch

                146




                146






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389661%2fchaining-app-initializer-functions-in-angular-6%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    TypeError: fit_transform() missing 1 required positional argument: 'X'