How to wait for dynamically loaded scripts to fully execute in javascript?









up vote
0
down vote

favorite












In javascript, I load several scripts, and then want to run a function after. The issue is, my promise runs too early. It runs after each file is downloaded, but I need it to wait until they are all added to DOM, and fully executed.



(function() 

function getFile(path)
return $.get(path, function (data)
$("body").append(data);
);


$.when.apply($, [
// load all the individual components
"components/once/ti-snackbar/ti-snackbar.html",
"components/once/ti-not-supported/ti-not-supported.html",
"components/once/ti-drawer/ti-drawer.html",
"components/widgets/ti-company-table-row/ti-company-table-row.html",
"components/widgets/ti-chip-set/ti-chip-set.html",
"components/widgets/ti-list/ti-list.html",
"components/widgets/ti-user-card/ti-user-card.html",
"components/widgets/ti-tabs/ti-tabs.html",
"components/widgets/ti-data-table/ti-data-table.html",
"components/forms/ti-new-company-form/ti-new-company-form.html",
"components/forms/ti-edit-company-form/ti-edit-company-form.html",
"components/pages/ti-page-inquire/ti-page-inquire.html",
"components/pages/ti-page-companies/ti-page-companies.html",
"components/pages/ti-page-report/ti-page-report.html",
"components/pages/ti-page-admins/ti-page-admins.html",
"components/pages/ti-page-contacts/ti-page-contacts.html",
"components/pages/ti-page-policy/ti-page-policy.html",
"components/pages/ti-page-manual/ti-page-manual.html",
"components/pages/ti-page-404/ti-page-404.html",
"components/tabs/ti-tab-name/ti-tab-name.html",
"components/tabs/ti-tab-tags/ti-tab-tags.html",
"components/tabs/ti-tab-status/ti-tab-status.html",
"components/tabs/ti-tab-restriction/ti-tab-restriction.html",
"components/tabs/ti-tab-other/ti-tab-other.html"
].map(getFile)).then(function()
// render the full app after all components load
getFile("components/once/ti-app/ti-app.html");
);
)();


How can I fix it?



Note: each html file has a <script> and <template>.



Thanks










share|improve this question

























    up vote
    0
    down vote

    favorite












    In javascript, I load several scripts, and then want to run a function after. The issue is, my promise runs too early. It runs after each file is downloaded, but I need it to wait until they are all added to DOM, and fully executed.



    (function() 

    function getFile(path)
    return $.get(path, function (data)
    $("body").append(data);
    );


    $.when.apply($, [
    // load all the individual components
    "components/once/ti-snackbar/ti-snackbar.html",
    "components/once/ti-not-supported/ti-not-supported.html",
    "components/once/ti-drawer/ti-drawer.html",
    "components/widgets/ti-company-table-row/ti-company-table-row.html",
    "components/widgets/ti-chip-set/ti-chip-set.html",
    "components/widgets/ti-list/ti-list.html",
    "components/widgets/ti-user-card/ti-user-card.html",
    "components/widgets/ti-tabs/ti-tabs.html",
    "components/widgets/ti-data-table/ti-data-table.html",
    "components/forms/ti-new-company-form/ti-new-company-form.html",
    "components/forms/ti-edit-company-form/ti-edit-company-form.html",
    "components/pages/ti-page-inquire/ti-page-inquire.html",
    "components/pages/ti-page-companies/ti-page-companies.html",
    "components/pages/ti-page-report/ti-page-report.html",
    "components/pages/ti-page-admins/ti-page-admins.html",
    "components/pages/ti-page-contacts/ti-page-contacts.html",
    "components/pages/ti-page-policy/ti-page-policy.html",
    "components/pages/ti-page-manual/ti-page-manual.html",
    "components/pages/ti-page-404/ti-page-404.html",
    "components/tabs/ti-tab-name/ti-tab-name.html",
    "components/tabs/ti-tab-tags/ti-tab-tags.html",
    "components/tabs/ti-tab-status/ti-tab-status.html",
    "components/tabs/ti-tab-restriction/ti-tab-restriction.html",
    "components/tabs/ti-tab-other/ti-tab-other.html"
    ].map(getFile)).then(function()
    // render the full app after all components load
    getFile("components/once/ti-app/ti-app.html");
    );
    )();


    How can I fix it?



    Note: each html file has a <script> and <template>.



    Thanks










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      In javascript, I load several scripts, and then want to run a function after. The issue is, my promise runs too early. It runs after each file is downloaded, but I need it to wait until they are all added to DOM, and fully executed.



      (function() 

      function getFile(path)
      return $.get(path, function (data)
      $("body").append(data);
      );


      $.when.apply($, [
      // load all the individual components
      "components/once/ti-snackbar/ti-snackbar.html",
      "components/once/ti-not-supported/ti-not-supported.html",
      "components/once/ti-drawer/ti-drawer.html",
      "components/widgets/ti-company-table-row/ti-company-table-row.html",
      "components/widgets/ti-chip-set/ti-chip-set.html",
      "components/widgets/ti-list/ti-list.html",
      "components/widgets/ti-user-card/ti-user-card.html",
      "components/widgets/ti-tabs/ti-tabs.html",
      "components/widgets/ti-data-table/ti-data-table.html",
      "components/forms/ti-new-company-form/ti-new-company-form.html",
      "components/forms/ti-edit-company-form/ti-edit-company-form.html",
      "components/pages/ti-page-inquire/ti-page-inquire.html",
      "components/pages/ti-page-companies/ti-page-companies.html",
      "components/pages/ti-page-report/ti-page-report.html",
      "components/pages/ti-page-admins/ti-page-admins.html",
      "components/pages/ti-page-contacts/ti-page-contacts.html",
      "components/pages/ti-page-policy/ti-page-policy.html",
      "components/pages/ti-page-manual/ti-page-manual.html",
      "components/pages/ti-page-404/ti-page-404.html",
      "components/tabs/ti-tab-name/ti-tab-name.html",
      "components/tabs/ti-tab-tags/ti-tab-tags.html",
      "components/tabs/ti-tab-status/ti-tab-status.html",
      "components/tabs/ti-tab-restriction/ti-tab-restriction.html",
      "components/tabs/ti-tab-other/ti-tab-other.html"
      ].map(getFile)).then(function()
      // render the full app after all components load
      getFile("components/once/ti-app/ti-app.html");
      );
      )();


      How can I fix it?



      Note: each html file has a <script> and <template>.



      Thanks










      share|improve this question













      In javascript, I load several scripts, and then want to run a function after. The issue is, my promise runs too early. It runs after each file is downloaded, but I need it to wait until they are all added to DOM, and fully executed.



      (function() 

      function getFile(path)
      return $.get(path, function (data)
      $("body").append(data);
      );


      $.when.apply($, [
      // load all the individual components
      "components/once/ti-snackbar/ti-snackbar.html",
      "components/once/ti-not-supported/ti-not-supported.html",
      "components/once/ti-drawer/ti-drawer.html",
      "components/widgets/ti-company-table-row/ti-company-table-row.html",
      "components/widgets/ti-chip-set/ti-chip-set.html",
      "components/widgets/ti-list/ti-list.html",
      "components/widgets/ti-user-card/ti-user-card.html",
      "components/widgets/ti-tabs/ti-tabs.html",
      "components/widgets/ti-data-table/ti-data-table.html",
      "components/forms/ti-new-company-form/ti-new-company-form.html",
      "components/forms/ti-edit-company-form/ti-edit-company-form.html",
      "components/pages/ti-page-inquire/ti-page-inquire.html",
      "components/pages/ti-page-companies/ti-page-companies.html",
      "components/pages/ti-page-report/ti-page-report.html",
      "components/pages/ti-page-admins/ti-page-admins.html",
      "components/pages/ti-page-contacts/ti-page-contacts.html",
      "components/pages/ti-page-policy/ti-page-policy.html",
      "components/pages/ti-page-manual/ti-page-manual.html",
      "components/pages/ti-page-404/ti-page-404.html",
      "components/tabs/ti-tab-name/ti-tab-name.html",
      "components/tabs/ti-tab-tags/ti-tab-tags.html",
      "components/tabs/ti-tab-status/ti-tab-status.html",
      "components/tabs/ti-tab-restriction/ti-tab-restriction.html",
      "components/tabs/ti-tab-other/ti-tab-other.html"
      ].map(getFile)).then(function()
      // render the full app after all components load
      getFile("components/once/ti-app/ti-app.html");
      );
      )();


      How can I fix it?



      Note: each html file has a <script> and <template>.



      Thanks







      javascript jquery ajax promise






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 6:29









      omega

      9,51544129257




      9,51544129257






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          I see several possible issues with this code:



          1. You aren't forcing your $.get() calls to finish in order so you can get a variable completion order and things may get appended to the body in varying order (if that matters).


          2. You are using a mix of promises and callbacks which can make it difficult to accurately sequence things. In general, don't mix promises and callbacks. If you have promises in one place, use them everywhere (convert callbacks to promises if necessary).


          3. You say you "want to run a function after", but you don't explain where you're trying to put that function. To wait for all the promises, it will have to be in a .then() handler.


          Here's an implementation that cleans these up:



          (function() 

          function getFile(path)
          return $.get(path);


          function append(data)
          $("body").append(data);


          var resources = [
          "components/once/ti-snackbar/ti-snackbar.html",
          "components/once/ti-not-supported/ti-not-supported.html",
          "components/once/ti-drawer/ti-drawer.html",
          "components/widgets/ti-company-table-row/ti-company-table-row.html",
          "components/widgets/ti-chip-set/ti-chip-set.html",
          "components/widgets/ti-list/ti-list.html",
          "components/widgets/ti-user-card/ti-user-card.html",
          "components/widgets/ti-tabs/ti-tabs.html",
          "components/widgets/ti-data-table/ti-data-table.html",
          "components/forms/ti-new-company-form/ti-new-company-form.html",
          "components/forms/ti-edit-company-form/ti-edit-company-form.html",
          "components/pages/ti-page-inquire/ti-page-inquire.html",
          "components/pages/ti-page-companies/ti-page-companies.html",
          "components/pages/ti-page-report/ti-page-report.html",
          "components/pages/ti-page-admins/ti-page-admins.html",
          "components/pages/ti-page-contacts/ti-page-contacts.html",
          "components/pages/ti-page-policy/ti-page-policy.html",
          "components/pages/ti-page-manual/ti-page-manual.html",
          "components/pages/ti-page-404/ti-page-404.html",
          "components/tabs/ti-tab-name/ti-tab-name.html",
          "components/tabs/ti-tab-tags/ti-tab-tags.html",
          "components/tabs/ti-tab-status/ti-tab-status.html",
          "components/tabs/ti-tab-restriction/ti-tab-restriction.html",
          "components/tabs/ti-tab-other/ti-tab-other.html"
          ];

          return Promise.all(resources.map(getFile)).then(function(data)
          // append all items to the body in order now that they are all retrieved
          data.forEach(append);
          ).then(function()
          // now that everything else is in place, load and append
          // the part that uses those scripts and templates
          return getFile("components/once/ti-app/ti-app.html").then(append);
          ).catch(function(err)
          // handle error here
          console.log(err);
          throw err; // propagate error
          );
          )().then(function()
          // everything loaded here for code outside the IIFE here to know when it's all done
          ).catch(function(err)
          // error occurred here for code outside the IIFE here to know there was an error
          );


          The last .then().catch() is optional, if you want code outside the IIFE to be able to know when things are done or had an error.



          Things modified and reasoning:



          1. All resources are loaded first and then appended in a predictable order which allows you to have interdependencies or any script initialization that can rely on previous dependencies. Before, there was no certain order that your scripts would be appended.


          2. ti-app.html won't be loaded and appended until all the other scripts are loaded and appended


          3. Moved the declaration of the array of scripts outside the main code flow just to improve the readability of the code flow.


          4. Added error detection


          5. Provide two places you can know when everything is done loading or if there was an error, one inside the IIFE and one outside the IIFE (depending upon what you need).






          share|improve this answer



























            up vote
            0
            down vote













            This could be useful: https://css-tricks.com/snippets/javascript/async-script-loader-with-callback/



            var Loader = function () 
            Loader.prototype = {
            require: function (scripts, callback)
            this.loadCount = 0;
            this.totalRequired = scripts.length;
            this.callback = callback;

            for (var i = 0; i < scripts.length; i++)
            this.writeScript(scripts[i]);

            ,
            loaded: function (evt)
            this.loadCount++;

            if (this.loadCount == this.totalRequired && typeof this.callback == 'function') this.callback.call();
            ,
            writeScript: function (src)
            var self = this;
            var s = document.createElement('script');
            s.type = "text/javascript";
            s.async = true;
            s.src = src;
            s.addEventListener('load', function (e) self.loaded(e); , false);
            var head = document.getElementsByTagName('head')[0];
            head.appendChild(s);



            Usage:



            var l = new Loader();
            l.require([
            "example-script-1.js",
            "example-script-2.js"],
            function()
            // Callback
            console.log('All Scripts Loaded');
            // call your other script here
            );





            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%2f53220844%2fhow-to-wait-for-dynamically-loaded-scripts-to-fully-execute-in-javascript%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote



              accepted










              I see several possible issues with this code:



              1. You aren't forcing your $.get() calls to finish in order so you can get a variable completion order and things may get appended to the body in varying order (if that matters).


              2. You are using a mix of promises and callbacks which can make it difficult to accurately sequence things. In general, don't mix promises and callbacks. If you have promises in one place, use them everywhere (convert callbacks to promises if necessary).


              3. You say you "want to run a function after", but you don't explain where you're trying to put that function. To wait for all the promises, it will have to be in a .then() handler.


              Here's an implementation that cleans these up:



              (function() 

              function getFile(path)
              return $.get(path);


              function append(data)
              $("body").append(data);


              var resources = [
              "components/once/ti-snackbar/ti-snackbar.html",
              "components/once/ti-not-supported/ti-not-supported.html",
              "components/once/ti-drawer/ti-drawer.html",
              "components/widgets/ti-company-table-row/ti-company-table-row.html",
              "components/widgets/ti-chip-set/ti-chip-set.html",
              "components/widgets/ti-list/ti-list.html",
              "components/widgets/ti-user-card/ti-user-card.html",
              "components/widgets/ti-tabs/ti-tabs.html",
              "components/widgets/ti-data-table/ti-data-table.html",
              "components/forms/ti-new-company-form/ti-new-company-form.html",
              "components/forms/ti-edit-company-form/ti-edit-company-form.html",
              "components/pages/ti-page-inquire/ti-page-inquire.html",
              "components/pages/ti-page-companies/ti-page-companies.html",
              "components/pages/ti-page-report/ti-page-report.html",
              "components/pages/ti-page-admins/ti-page-admins.html",
              "components/pages/ti-page-contacts/ti-page-contacts.html",
              "components/pages/ti-page-policy/ti-page-policy.html",
              "components/pages/ti-page-manual/ti-page-manual.html",
              "components/pages/ti-page-404/ti-page-404.html",
              "components/tabs/ti-tab-name/ti-tab-name.html",
              "components/tabs/ti-tab-tags/ti-tab-tags.html",
              "components/tabs/ti-tab-status/ti-tab-status.html",
              "components/tabs/ti-tab-restriction/ti-tab-restriction.html",
              "components/tabs/ti-tab-other/ti-tab-other.html"
              ];

              return Promise.all(resources.map(getFile)).then(function(data)
              // append all items to the body in order now that they are all retrieved
              data.forEach(append);
              ).then(function()
              // now that everything else is in place, load and append
              // the part that uses those scripts and templates
              return getFile("components/once/ti-app/ti-app.html").then(append);
              ).catch(function(err)
              // handle error here
              console.log(err);
              throw err; // propagate error
              );
              )().then(function()
              // everything loaded here for code outside the IIFE here to know when it's all done
              ).catch(function(err)
              // error occurred here for code outside the IIFE here to know there was an error
              );


              The last .then().catch() is optional, if you want code outside the IIFE to be able to know when things are done or had an error.



              Things modified and reasoning:



              1. All resources are loaded first and then appended in a predictable order which allows you to have interdependencies or any script initialization that can rely on previous dependencies. Before, there was no certain order that your scripts would be appended.


              2. ti-app.html won't be loaded and appended until all the other scripts are loaded and appended


              3. Moved the declaration of the array of scripts outside the main code flow just to improve the readability of the code flow.


              4. Added error detection


              5. Provide two places you can know when everything is done loading or if there was an error, one inside the IIFE and one outside the IIFE (depending upon what you need).






              share|improve this answer
























                up vote
                1
                down vote



                accepted










                I see several possible issues with this code:



                1. You aren't forcing your $.get() calls to finish in order so you can get a variable completion order and things may get appended to the body in varying order (if that matters).


                2. You are using a mix of promises and callbacks which can make it difficult to accurately sequence things. In general, don't mix promises and callbacks. If you have promises in one place, use them everywhere (convert callbacks to promises if necessary).


                3. You say you "want to run a function after", but you don't explain where you're trying to put that function. To wait for all the promises, it will have to be in a .then() handler.


                Here's an implementation that cleans these up:



                (function() 

                function getFile(path)
                return $.get(path);


                function append(data)
                $("body").append(data);


                var resources = [
                "components/once/ti-snackbar/ti-snackbar.html",
                "components/once/ti-not-supported/ti-not-supported.html",
                "components/once/ti-drawer/ti-drawer.html",
                "components/widgets/ti-company-table-row/ti-company-table-row.html",
                "components/widgets/ti-chip-set/ti-chip-set.html",
                "components/widgets/ti-list/ti-list.html",
                "components/widgets/ti-user-card/ti-user-card.html",
                "components/widgets/ti-tabs/ti-tabs.html",
                "components/widgets/ti-data-table/ti-data-table.html",
                "components/forms/ti-new-company-form/ti-new-company-form.html",
                "components/forms/ti-edit-company-form/ti-edit-company-form.html",
                "components/pages/ti-page-inquire/ti-page-inquire.html",
                "components/pages/ti-page-companies/ti-page-companies.html",
                "components/pages/ti-page-report/ti-page-report.html",
                "components/pages/ti-page-admins/ti-page-admins.html",
                "components/pages/ti-page-contacts/ti-page-contacts.html",
                "components/pages/ti-page-policy/ti-page-policy.html",
                "components/pages/ti-page-manual/ti-page-manual.html",
                "components/pages/ti-page-404/ti-page-404.html",
                "components/tabs/ti-tab-name/ti-tab-name.html",
                "components/tabs/ti-tab-tags/ti-tab-tags.html",
                "components/tabs/ti-tab-status/ti-tab-status.html",
                "components/tabs/ti-tab-restriction/ti-tab-restriction.html",
                "components/tabs/ti-tab-other/ti-tab-other.html"
                ];

                return Promise.all(resources.map(getFile)).then(function(data)
                // append all items to the body in order now that they are all retrieved
                data.forEach(append);
                ).then(function()
                // now that everything else is in place, load and append
                // the part that uses those scripts and templates
                return getFile("components/once/ti-app/ti-app.html").then(append);
                ).catch(function(err)
                // handle error here
                console.log(err);
                throw err; // propagate error
                );
                )().then(function()
                // everything loaded here for code outside the IIFE here to know when it's all done
                ).catch(function(err)
                // error occurred here for code outside the IIFE here to know there was an error
                );


                The last .then().catch() is optional, if you want code outside the IIFE to be able to know when things are done or had an error.



                Things modified and reasoning:



                1. All resources are loaded first and then appended in a predictable order which allows you to have interdependencies or any script initialization that can rely on previous dependencies. Before, there was no certain order that your scripts would be appended.


                2. ti-app.html won't be loaded and appended until all the other scripts are loaded and appended


                3. Moved the declaration of the array of scripts outside the main code flow just to improve the readability of the code flow.


                4. Added error detection


                5. Provide two places you can know when everything is done loading or if there was an error, one inside the IIFE and one outside the IIFE (depending upon what you need).






                share|improve this answer






















                  up vote
                  1
                  down vote



                  accepted







                  up vote
                  1
                  down vote



                  accepted






                  I see several possible issues with this code:



                  1. You aren't forcing your $.get() calls to finish in order so you can get a variable completion order and things may get appended to the body in varying order (if that matters).


                  2. You are using a mix of promises and callbacks which can make it difficult to accurately sequence things. In general, don't mix promises and callbacks. If you have promises in one place, use them everywhere (convert callbacks to promises if necessary).


                  3. You say you "want to run a function after", but you don't explain where you're trying to put that function. To wait for all the promises, it will have to be in a .then() handler.


                  Here's an implementation that cleans these up:



                  (function() 

                  function getFile(path)
                  return $.get(path);


                  function append(data)
                  $("body").append(data);


                  var resources = [
                  "components/once/ti-snackbar/ti-snackbar.html",
                  "components/once/ti-not-supported/ti-not-supported.html",
                  "components/once/ti-drawer/ti-drawer.html",
                  "components/widgets/ti-company-table-row/ti-company-table-row.html",
                  "components/widgets/ti-chip-set/ti-chip-set.html",
                  "components/widgets/ti-list/ti-list.html",
                  "components/widgets/ti-user-card/ti-user-card.html",
                  "components/widgets/ti-tabs/ti-tabs.html",
                  "components/widgets/ti-data-table/ti-data-table.html",
                  "components/forms/ti-new-company-form/ti-new-company-form.html",
                  "components/forms/ti-edit-company-form/ti-edit-company-form.html",
                  "components/pages/ti-page-inquire/ti-page-inquire.html",
                  "components/pages/ti-page-companies/ti-page-companies.html",
                  "components/pages/ti-page-report/ti-page-report.html",
                  "components/pages/ti-page-admins/ti-page-admins.html",
                  "components/pages/ti-page-contacts/ti-page-contacts.html",
                  "components/pages/ti-page-policy/ti-page-policy.html",
                  "components/pages/ti-page-manual/ti-page-manual.html",
                  "components/pages/ti-page-404/ti-page-404.html",
                  "components/tabs/ti-tab-name/ti-tab-name.html",
                  "components/tabs/ti-tab-tags/ti-tab-tags.html",
                  "components/tabs/ti-tab-status/ti-tab-status.html",
                  "components/tabs/ti-tab-restriction/ti-tab-restriction.html",
                  "components/tabs/ti-tab-other/ti-tab-other.html"
                  ];

                  return Promise.all(resources.map(getFile)).then(function(data)
                  // append all items to the body in order now that they are all retrieved
                  data.forEach(append);
                  ).then(function()
                  // now that everything else is in place, load and append
                  // the part that uses those scripts and templates
                  return getFile("components/once/ti-app/ti-app.html").then(append);
                  ).catch(function(err)
                  // handle error here
                  console.log(err);
                  throw err; // propagate error
                  );
                  )().then(function()
                  // everything loaded here for code outside the IIFE here to know when it's all done
                  ).catch(function(err)
                  // error occurred here for code outside the IIFE here to know there was an error
                  );


                  The last .then().catch() is optional, if you want code outside the IIFE to be able to know when things are done or had an error.



                  Things modified and reasoning:



                  1. All resources are loaded first and then appended in a predictable order which allows you to have interdependencies or any script initialization that can rely on previous dependencies. Before, there was no certain order that your scripts would be appended.


                  2. ti-app.html won't be loaded and appended until all the other scripts are loaded and appended


                  3. Moved the declaration of the array of scripts outside the main code flow just to improve the readability of the code flow.


                  4. Added error detection


                  5. Provide two places you can know when everything is done loading or if there was an error, one inside the IIFE and one outside the IIFE (depending upon what you need).






                  share|improve this answer












                  I see several possible issues with this code:



                  1. You aren't forcing your $.get() calls to finish in order so you can get a variable completion order and things may get appended to the body in varying order (if that matters).


                  2. You are using a mix of promises and callbacks which can make it difficult to accurately sequence things. In general, don't mix promises and callbacks. If you have promises in one place, use them everywhere (convert callbacks to promises if necessary).


                  3. You say you "want to run a function after", but you don't explain where you're trying to put that function. To wait for all the promises, it will have to be in a .then() handler.


                  Here's an implementation that cleans these up:



                  (function() 

                  function getFile(path)
                  return $.get(path);


                  function append(data)
                  $("body").append(data);


                  var resources = [
                  "components/once/ti-snackbar/ti-snackbar.html",
                  "components/once/ti-not-supported/ti-not-supported.html",
                  "components/once/ti-drawer/ti-drawer.html",
                  "components/widgets/ti-company-table-row/ti-company-table-row.html",
                  "components/widgets/ti-chip-set/ti-chip-set.html",
                  "components/widgets/ti-list/ti-list.html",
                  "components/widgets/ti-user-card/ti-user-card.html",
                  "components/widgets/ti-tabs/ti-tabs.html",
                  "components/widgets/ti-data-table/ti-data-table.html",
                  "components/forms/ti-new-company-form/ti-new-company-form.html",
                  "components/forms/ti-edit-company-form/ti-edit-company-form.html",
                  "components/pages/ti-page-inquire/ti-page-inquire.html",
                  "components/pages/ti-page-companies/ti-page-companies.html",
                  "components/pages/ti-page-report/ti-page-report.html",
                  "components/pages/ti-page-admins/ti-page-admins.html",
                  "components/pages/ti-page-contacts/ti-page-contacts.html",
                  "components/pages/ti-page-policy/ti-page-policy.html",
                  "components/pages/ti-page-manual/ti-page-manual.html",
                  "components/pages/ti-page-404/ti-page-404.html",
                  "components/tabs/ti-tab-name/ti-tab-name.html",
                  "components/tabs/ti-tab-tags/ti-tab-tags.html",
                  "components/tabs/ti-tab-status/ti-tab-status.html",
                  "components/tabs/ti-tab-restriction/ti-tab-restriction.html",
                  "components/tabs/ti-tab-other/ti-tab-other.html"
                  ];

                  return Promise.all(resources.map(getFile)).then(function(data)
                  // append all items to the body in order now that they are all retrieved
                  data.forEach(append);
                  ).then(function()
                  // now that everything else is in place, load and append
                  // the part that uses those scripts and templates
                  return getFile("components/once/ti-app/ti-app.html").then(append);
                  ).catch(function(err)
                  // handle error here
                  console.log(err);
                  throw err; // propagate error
                  );
                  )().then(function()
                  // everything loaded here for code outside the IIFE here to know when it's all done
                  ).catch(function(err)
                  // error occurred here for code outside the IIFE here to know there was an error
                  );


                  The last .then().catch() is optional, if you want code outside the IIFE to be able to know when things are done or had an error.



                  Things modified and reasoning:



                  1. All resources are loaded first and then appended in a predictable order which allows you to have interdependencies or any script initialization that can rely on previous dependencies. Before, there was no certain order that your scripts would be appended.


                  2. ti-app.html won't be loaded and appended until all the other scripts are loaded and appended


                  3. Moved the declaration of the array of scripts outside the main code flow just to improve the readability of the code flow.


                  4. Added error detection


                  5. Provide two places you can know when everything is done loading or if there was an error, one inside the IIFE and one outside the IIFE (depending upon what you need).







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 9 at 16:55









                  jfriend00

                  424k51540586




                  424k51540586






















                      up vote
                      0
                      down vote













                      This could be useful: https://css-tricks.com/snippets/javascript/async-script-loader-with-callback/



                      var Loader = function () 
                      Loader.prototype = {
                      require: function (scripts, callback)
                      this.loadCount = 0;
                      this.totalRequired = scripts.length;
                      this.callback = callback;

                      for (var i = 0; i < scripts.length; i++)
                      this.writeScript(scripts[i]);

                      ,
                      loaded: function (evt)
                      this.loadCount++;

                      if (this.loadCount == this.totalRequired && typeof this.callback == 'function') this.callback.call();
                      ,
                      writeScript: function (src)
                      var self = this;
                      var s = document.createElement('script');
                      s.type = "text/javascript";
                      s.async = true;
                      s.src = src;
                      s.addEventListener('load', function (e) self.loaded(e); , false);
                      var head = document.getElementsByTagName('head')[0];
                      head.appendChild(s);



                      Usage:



                      var l = new Loader();
                      l.require([
                      "example-script-1.js",
                      "example-script-2.js"],
                      function()
                      // Callback
                      console.log('All Scripts Loaded');
                      // call your other script here
                      );





                      share|improve this answer
























                        up vote
                        0
                        down vote













                        This could be useful: https://css-tricks.com/snippets/javascript/async-script-loader-with-callback/



                        var Loader = function () 
                        Loader.prototype = {
                        require: function (scripts, callback)
                        this.loadCount = 0;
                        this.totalRequired = scripts.length;
                        this.callback = callback;

                        for (var i = 0; i < scripts.length; i++)
                        this.writeScript(scripts[i]);

                        ,
                        loaded: function (evt)
                        this.loadCount++;

                        if (this.loadCount == this.totalRequired && typeof this.callback == 'function') this.callback.call();
                        ,
                        writeScript: function (src)
                        var self = this;
                        var s = document.createElement('script');
                        s.type = "text/javascript";
                        s.async = true;
                        s.src = src;
                        s.addEventListener('load', function (e) self.loaded(e); , false);
                        var head = document.getElementsByTagName('head')[0];
                        head.appendChild(s);



                        Usage:



                        var l = new Loader();
                        l.require([
                        "example-script-1.js",
                        "example-script-2.js"],
                        function()
                        // Callback
                        console.log('All Scripts Loaded');
                        // call your other script here
                        );





                        share|improve this answer






















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          This could be useful: https://css-tricks.com/snippets/javascript/async-script-loader-with-callback/



                          var Loader = function () 
                          Loader.prototype = {
                          require: function (scripts, callback)
                          this.loadCount = 0;
                          this.totalRequired = scripts.length;
                          this.callback = callback;

                          for (var i = 0; i < scripts.length; i++)
                          this.writeScript(scripts[i]);

                          ,
                          loaded: function (evt)
                          this.loadCount++;

                          if (this.loadCount == this.totalRequired && typeof this.callback == 'function') this.callback.call();
                          ,
                          writeScript: function (src)
                          var self = this;
                          var s = document.createElement('script');
                          s.type = "text/javascript";
                          s.async = true;
                          s.src = src;
                          s.addEventListener('load', function (e) self.loaded(e); , false);
                          var head = document.getElementsByTagName('head')[0];
                          head.appendChild(s);



                          Usage:



                          var l = new Loader();
                          l.require([
                          "example-script-1.js",
                          "example-script-2.js"],
                          function()
                          // Callback
                          console.log('All Scripts Loaded');
                          // call your other script here
                          );





                          share|improve this answer












                          This could be useful: https://css-tricks.com/snippets/javascript/async-script-loader-with-callback/



                          var Loader = function () 
                          Loader.prototype = {
                          require: function (scripts, callback)
                          this.loadCount = 0;
                          this.totalRequired = scripts.length;
                          this.callback = callback;

                          for (var i = 0; i < scripts.length; i++)
                          this.writeScript(scripts[i]);

                          ,
                          loaded: function (evt)
                          this.loadCount++;

                          if (this.loadCount == this.totalRequired && typeof this.callback == 'function') this.callback.call();
                          ,
                          writeScript: function (src)
                          var self = this;
                          var s = document.createElement('script');
                          s.type = "text/javascript";
                          s.async = true;
                          s.src = src;
                          s.addEventListener('load', function (e) self.loaded(e); , false);
                          var head = document.getElementsByTagName('head')[0];
                          head.appendChild(s);



                          Usage:



                          var l = new Loader();
                          l.require([
                          "example-script-1.js",
                          "example-script-2.js"],
                          function()
                          // Callback
                          console.log('All Scripts Loaded');
                          // call your other script here
                          );






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 9 at 6:49









                          Eriks Klotins

                          863516




                          863516



























                              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%2f53220844%2fhow-to-wait-for-dynamically-loaded-scripts-to-fully-execute-in-javascript%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

                              𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                              Edmonton

                              Crossroads (UK TV series)