title: How to call a helper function within a Promise'd function
body: <p>I am still new to Javascript and I'm hoping someone out there can help me with a problem I'm having that I just haven't been able to wrap my head around!!</p>
  
  <p>I have a function with a lot of duplicate code in it, so tried to spin it off into a helper function. However when I call the helper function from within the lMS function (which is currently being called as <code>lMS(f).then(</code>), the helper function executes AFTER the <code>init(g)</code> function which is reliant on lMS executing and completing first.</p>
  
  <p>I'm 99% sure that I'm having this issue due to my misunderstanding of how <code>Promise</code>s work and the nature of async functions. </p>
  
  <p>I have tried pushing the duplicate code into a separate function and calling that when it is needed. I have tried capturing the responses of that as Promises, or pushing the responses into an array and then only executing when I have all items returned (compared to the original array).</p>
  
  <p>The following is the <em>original</em> script, where there is no helper function but a good chunk of duplicate code: <a href="https://github.com/willstocks-tech/dynamically-polyfill-features-for-a-script/releases/tag/0.0.5b5" rel="nofollow noreferrer">https://github.com/willstocks-tech/dynamically-polyfill-features-for-a-script/releases/tag/0.0.5b5</a> - line 57 (<code>function loadMyScript(url)</code>)</p>
  
  <p>I have put the helper code into a Codepen (I've been working on it for a few days) <a href="https://codepen.io/willstocks_tech/pen/pGgRrG?editors=1012" rel="nofollow noreferrer">https://codepen.io/willstocks_tech/pen/pGgRrG?editors=1012</a></p>
  
  <h2>Update</h2>
  
  <p>Included <code>init</code> function within "helper function" code and a new pen to detail everything I have tried/am currently trying based on feedback:
  <a href="https://codepen.io/willstocks_tech/pen/YBEzLW?editors=1012" rel="nofollow noreferrer">https://codepen.io/willstocks_tech/pen/YBEzLW?editors=1012</a></p>
  
  <p>Current code:</p>
  
  <pre><code>function lMS(f) {
      if(Array.isArray(f)) {
          var urlen = f.length;
          for (var u = 0; u &lt; urlen; u++) {
              var uri = f[u];
              if(uri !== null &amp;&amp; uri !== '') {
                  return new Promise(
                      function(resolve, reject) {
                          var thescript = document.createElement('script');
                          thescript.src = encodeURI(uri);
                          document.body.appendChild(thescript);
                          thescript.onerror = function(response) {
                              return reject("Loading the script failed!", response);
                          } 
                          thescript.onload = function() {
                              return resolve("Script setup and ready to load!");
                          } 
                      }
                  )
              } else {
                  return new Promise( //pretty sure this could just be Promise.resolve();
                      function(resolve, reject) {
                          return resolve ("No script to load");
                      }
                  )
              }
          }
      } else {
          if(f !== null &amp;&amp; f !== '') {
              return new Promise(
                  function(resolve, reject) {
                      var thescript = document.createElement('script');
                      thescript.src = encodeURI(f);
                      document.body.appendChild(thescript);
                      thescript.onerror = function(response) {
                          return reject("Loading the script failed!", response);
                      } 
                      thescript.onload = function() {
                          return resolve("Script setup and ready to load!");
                      } 
                  }
              )
          } else {
              return new Promise( //pretty sure this could just be Promise.resolve();
                  function(resolve, reject) {
                      return resolve ("No script to load");
                  }
              )
          }
      }
  }
  </code></pre>
  
  <p>New work in progress (with helper):</p>
  
  <pre><code>function pL(e, f, g) {
      cNS(e).then(
          function() {
              lMS(f, g)
          }
      ).catch(function(error){return error})
      }
  }
  
  function lMS(f, g) {
      var w = [];
      if(Array.isArray(f)) {
          var urlen = f.length;
          for (var u = 0; u &lt; urlen; u++) {
              var uri = f[u];
              if(uri !== null &amp;&amp; uri !== '') {
                  uriProm(uri); //go and get a script that is needed
                  w.push(uri);  //maybe push to array and return resolve once everything is done?
              } else {
                  return;
              }
          }
          if(w.length === url.length &amp;&amp; w.every(function(value, index) { return value === url[index]})) {
              console.log("We've made it to here");
              return init(g) //go off to run a piece of code based reliant on the script of uriProm
              }
      } else { //not an array of values
          if(url !== null &amp;&amp; url !== '') {
              uriProm(uri);
              return init(g)
          } else {
              return
          }
      }
  }
  
  //helper function (avoiding duplicate code)
  function uriProm(uri){
      var thescript = document.createElement('script');
      thescript.src = encodeURI(uri);
      document.body.appendChild(thescript);
      thescript.onerror = function(response) {
          return reject("Loading the script failed!", response);
      } 
      thescript.onload = function() {
          return Promise.resolve();
      } 
  }
  
      function init(g) {
          if(Array.isArray(g)) {
              var fnlen = g.length;
              for (var f = 0; f &lt; fnlen; f++) {
                  try {
                      new Function(g[f])();
                  } catch(err) {
                      console.error('There was an error: ', err.name, err.stack);
                  }
              }           
          } else {    
              try {
                  new Function(g)();
              } catch(err) {
                  console.error('There was an error: ', err.name, err.stack);
              }
          }
      }
  </code></pre>