Angular 7: trying to delay router.navigate with setTimeout not working










0














I'm having an issue with my sign in handler redirecting to a page before the cookie with the user token is written. As a workaround, I tried adding a delay before the navigation, but it isn't working.



Why is the navigation happening before the timeout delay? (This is Angular 7 Universal.) I also tried setting the token in the delay function, but it didn't make a difference.



signInSuccessHandler() 
...
const token = this.getToken();
this.setToken(access_token);
setTimeout(() => this.delayNavigation(); , 1000);


delayNavigation()
this._zone.run(() => this.router.navigate(['/people']));










share|improve this question



















  • 1




    Are you sure it's not waiting for the timeout?
    – user184994
    Nov 1 at 19:04






  • 1




    you can make this.setToken(access_toke) return a promise, and wait until it resolves, after that you would do a redirect.
    – Yuriy Kravets
    Nov 1 at 19:04






  • 2




    it's not the answer, but Why not navigate after setToken?. If your setToken return an observable -you can simple use return Observable.of(true)- you can redirect in subscribe. If is more comples your setToken, after SetToken emit a value observables and subscribe to the Observable. The "angular way" is NOT wait a time and expect the given time was enought to the things happens
    – Eliseo
    Nov 1 at 19:05










  • @YuriyKravets that’s a great idea. Thanks!
    – beachCode
    Nov 1 at 19:05















0














I'm having an issue with my sign in handler redirecting to a page before the cookie with the user token is written. As a workaround, I tried adding a delay before the navigation, but it isn't working.



Why is the navigation happening before the timeout delay? (This is Angular 7 Universal.) I also tried setting the token in the delay function, but it didn't make a difference.



signInSuccessHandler() 
...
const token = this.getToken();
this.setToken(access_token);
setTimeout(() => this.delayNavigation(); , 1000);


delayNavigation()
this._zone.run(() => this.router.navigate(['/people']));










share|improve this question



















  • 1




    Are you sure it's not waiting for the timeout?
    – user184994
    Nov 1 at 19:04






  • 1




    you can make this.setToken(access_toke) return a promise, and wait until it resolves, after that you would do a redirect.
    – Yuriy Kravets
    Nov 1 at 19:04






  • 2




    it's not the answer, but Why not navigate after setToken?. If your setToken return an observable -you can simple use return Observable.of(true)- you can redirect in subscribe. If is more comples your setToken, after SetToken emit a value observables and subscribe to the Observable. The "angular way" is NOT wait a time and expect the given time was enought to the things happens
    – Eliseo
    Nov 1 at 19:05










  • @YuriyKravets that’s a great idea. Thanks!
    – beachCode
    Nov 1 at 19:05













0












0








0







I'm having an issue with my sign in handler redirecting to a page before the cookie with the user token is written. As a workaround, I tried adding a delay before the navigation, but it isn't working.



Why is the navigation happening before the timeout delay? (This is Angular 7 Universal.) I also tried setting the token in the delay function, but it didn't make a difference.



signInSuccessHandler() 
...
const token = this.getToken();
this.setToken(access_token);
setTimeout(() => this.delayNavigation(); , 1000);


delayNavigation()
this._zone.run(() => this.router.navigate(['/people']));










share|improve this question















I'm having an issue with my sign in handler redirecting to a page before the cookie with the user token is written. As a workaround, I tried adding a delay before the navigation, but it isn't working.



Why is the navigation happening before the timeout delay? (This is Angular 7 Universal.) I also tried setting the token in the delay function, but it didn't make a difference.



signInSuccessHandler() 
...
const token = this.getToken();
this.setToken(access_token);
setTimeout(() => this.delayNavigation(); , 1000);


delayNavigation()
this._zone.run(() => this.router.navigate(['/people']));







javascript angular angular-universal angular7






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 7:09









Goncalo Peres

1,3091317




1,3091317










asked Nov 1 at 18:58









beachCode

76711528




76711528







  • 1




    Are you sure it's not waiting for the timeout?
    – user184994
    Nov 1 at 19:04






  • 1




    you can make this.setToken(access_toke) return a promise, and wait until it resolves, after that you would do a redirect.
    – Yuriy Kravets
    Nov 1 at 19:04






  • 2




    it's not the answer, but Why not navigate after setToken?. If your setToken return an observable -you can simple use return Observable.of(true)- you can redirect in subscribe. If is more comples your setToken, after SetToken emit a value observables and subscribe to the Observable. The "angular way" is NOT wait a time and expect the given time was enought to the things happens
    – Eliseo
    Nov 1 at 19:05










  • @YuriyKravets that’s a great idea. Thanks!
    – beachCode
    Nov 1 at 19:05












  • 1




    Are you sure it's not waiting for the timeout?
    – user184994
    Nov 1 at 19:04






  • 1




    you can make this.setToken(access_toke) return a promise, and wait until it resolves, after that you would do a redirect.
    – Yuriy Kravets
    Nov 1 at 19:04






  • 2




    it's not the answer, but Why not navigate after setToken?. If your setToken return an observable -you can simple use return Observable.of(true)- you can redirect in subscribe. If is more comples your setToken, after SetToken emit a value observables and subscribe to the Observable. The "angular way" is NOT wait a time and expect the given time was enought to the things happens
    – Eliseo
    Nov 1 at 19:05










  • @YuriyKravets that’s a great idea. Thanks!
    – beachCode
    Nov 1 at 19:05







1




1




Are you sure it's not waiting for the timeout?
– user184994
Nov 1 at 19:04




Are you sure it's not waiting for the timeout?
– user184994
Nov 1 at 19:04




1




1




you can make this.setToken(access_toke) return a promise, and wait until it resolves, after that you would do a redirect.
– Yuriy Kravets
Nov 1 at 19:04




you can make this.setToken(access_toke) return a promise, and wait until it resolves, after that you would do a redirect.
– Yuriy Kravets
Nov 1 at 19:04




2




2




it's not the answer, but Why not navigate after setToken?. If your setToken return an observable -you can simple use return Observable.of(true)- you can redirect in subscribe. If is more comples your setToken, after SetToken emit a value observables and subscribe to the Observable. The "angular way" is NOT wait a time and expect the given time was enought to the things happens
– Eliseo
Nov 1 at 19:05




it's not the answer, but Why not navigate after setToken?. If your setToken return an observable -you can simple use return Observable.of(true)- you can redirect in subscribe. If is more comples your setToken, after SetToken emit a value observables and subscribe to the Observable. The "angular way" is NOT wait a time and expect the given time was enought to the things happens
– Eliseo
Nov 1 at 19:05












@YuriyKravets that’s a great idea. Thanks!
– beachCode
Nov 1 at 19:05




@YuriyKravets that’s a great idea. Thanks!
– beachCode
Nov 1 at 19:05












1 Answer
1






active

oldest

votes


















0














Thanks for the replies. I found the underlying cause, so I don't need the workaround anymore.



A service was being instantiated before the token was available. The timeout didn't help because the issue had already happened.






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',
    autoActivateHeartbeat: false,
    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%2f53107679%2fangular-7-trying-to-delay-router-navigate-with-settimeout-not-working%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









    0














    Thanks for the replies. I found the underlying cause, so I don't need the workaround anymore.



    A service was being instantiated before the token was available. The timeout didn't help because the issue had already happened.






    share|improve this answer

























      0














      Thanks for the replies. I found the underlying cause, so I don't need the workaround anymore.



      A service was being instantiated before the token was available. The timeout didn't help because the issue had already happened.






      share|improve this answer























        0












        0








        0






        Thanks for the replies. I found the underlying cause, so I don't need the workaround anymore.



        A service was being instantiated before the token was available. The timeout didn't help because the issue had already happened.






        share|improve this answer












        Thanks for the replies. I found the underlying cause, so I don't need the workaround anymore.



        A service was being instantiated before the token was available. The timeout didn't help because the issue had already happened.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 1 at 23:06









        beachCode

        76711528




        76711528



























            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%2f53107679%2fangular-7-trying-to-delay-router-navigate-with-settimeout-not-working%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

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20