title: Proper way to wait for one function to finish before continuing?
body: <p>I have two JS functions. One calls the other. Within the calling function, I'd like to call the other, wait for that function to finish, then continue on. So, for example/pseudo code:</p>
  
  <pre><code>function firstFunction(){
      for(i=0;i&lt;x;i++){
          // do something
      }
  };
  
  function secondFunction(){
      firstFunction()
      // now wait for firstFunction to finish...
      // do something else
  };
  </code></pre>
  
  <p>I came up with this solution, but don't know if this is a smart way to go about it.</p>
  
  <pre><code>var isPaused = false;
  
  function firstFunction(){
      isPaused = true;
      for(i=0;i&lt;x;i++){
          // do something
      }
      isPaused = false;
  };
  
  function secondFunction(){
      firstFunction()
      function waitForIt(){
          if (isPaused) {
              setTimeout(function(){waitForIt()},100);
          } else {
              // go do that thing
          };
      }
  };
  </code></pre>
  
  <p>Is that legit? Is there a more elegant way to handle it? Perhaps with jQuery?</p>