title: How to have one function wait for another function to finish processing in javascript using chaining?
body: <p>I have a function that pulls an array from firebase via its rest api, which I need to input into another function to create a calendar. </p>
  
  <pre><code>function array_from_firebase(){
      //code that pulls from firebase
      return array
  }
  
  function calendar_create_from_array(array){
      //code that generates calendar
  }
  </code></pre>
  
  <p>The following does not work:</p>
  
  <pre><code>calendar_create_from_array(array_from_firebase())
  </code></pre>
  
  <p>However, this does work</p>
  
  <pre><code>array = array_from_firebase()
  setTimeout(calendar_create_from_array,9000,array)
  </code></pre>
  
  <p>I believe this means that array_from_firebase takes a bit of time longer than calendar_create_from_array  and calendar_create_from_array triggers too quickly. </p>
  
  <p>How could I use chaining and promises to solve this?</p>